這是我寫的一支小程式,目的是用來把卡鐘產出的考勤資料Excel檔,
轉換成特定格式的純文字檔,後續再把資料餵給資料庫。

程式碼如下:

static void Main(string[] args)
{
    string filePath = "D:\\card-data\\grezj\\";
    DateTime dt = DateTime.Now;
    string inFile = dt.ToString("yyyy.MM.dd") + ".XLS";
    string ouFile = DateTime.Now.ToString("yyyyMMdd");
    try
    {
        StreamWriter sw = new StreamWriter(filePath + ouFile);
        string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filePath + inFile + ";Extended Properties='Excel 8.0;HDR=YES'";
        OleDbConnection oleCon = new OleDbConnection(strCon);
        string strCom = " SELECT * FROM [Sheet1$] ";
        OleDbCommand oleCmd = new OleDbCommand(strCom, oleCon);
        oleCon.Open();
        OleDbDataReader oleReader = oleCmd.ExecuteReader();
        while (oleReader.Read())
        {
            string workCard = "ZJ" + oleReader[4].ToString().Substring(2, 4);
            string cardIO = "2";
            string cardDate = oleReader[5].ToString();
            string cardTime = oleReader[6].ToString().Replace(":","");
            string cardDCK = "1";
            sw.WriteLine(workCard + cardIO + cardDate + cardTime + cardDCK);
        }             
        sw.Close();
        oleCon.Close();
    }
    catch (Exception e)
    {
        Environment.Exit(0);
    }     
}

程式撰寫完畢之後,在我的電腦(32bit win7)運作正常,
丟到Server(64bit win2008r2)上卻無法運作,
抓一下bug發現拋出的Exception為「Microsoft.Jet.OLEDB.4.0 提供者並未登錄於本機電腦上」,
google一下才知道原來Microsoft.Jet.OLEDB.4.0只支援32bit應用程式,
於是我到[專案]->[屬性]->[建置]的目標平台,把目標平台的[Any CPU]改成[x86]就可以了。

我猜應該是Visual Studio在編譯的時候,若用預設的Any CPU,
編譯完的程式會根據作業系統自行判斷要以32bit或64bit來運作,
所以才會造成我32bit可以運作,丟到64bit上就無法運作的情況,
後來我把目標平台改成x86來編譯,等於是強制要求機器必須以32bit來運作,
當然...這支程式不管丟到32bit或64bit的作業系統,就都可以正常運作了。

arrow
arrow
    全站熱搜

    浮雲 發表在 痞客邦 留言(0) 人氣()