我想通過oledb連接使用以下代碼將當前時間插入到Excel中的Time列中,但是當我檢查Excel時,插入的值是Date格式。無法使用oledb更新excel中的當前時間使用oledb
值在Excel中更新 - 1/0/1900下午3時54分11秒
期望值 - 下午3時54分11秒
string currentTime = DateTime.Now.ToString("hh:mm:ss.fff tt");
string cmnd1 = "Create Table [" + currentDate + "] (TestCase char(100), ExecutionTime Time, Result char(20))";
string cmnd2 = "Insert Into [" + currentDate + "] (TestCase, ExecutionTime, Result) values ("+ "'" + tName + "',@dd,'" + result +"')" ;
using (OleDbConnection conn = new OleDbConnection(ConnectionStringtd))
{
OleDbCommand createSheet = new OleDbCommand(cmnd1, conn);
OleDbCommand insertResult = new OleDbCommand(cmnd2, conn);
insertResult.Parameters.AddWithValue("@dd", DateTime.Now.TimeOfDay);
conn.Open();
try
{
createSheet.ExecuteNonQuery();
}
catch(OleDbException) {}
insertResult.ExecuteNonQuery();
}
}
'ExecutionTime'列的類型是什麼? –
請勿使用AddWithValue。該方法自行推斷參數的類型,使用日期時間值很容易失敗。使用添加並指定OleDbType。嘗試各種選項。順便說一下,爲什麼只爲其他值使用一個參數和字符串concatantion? – Steve
另外:空Try/Catch是一個錯誤。如果第一個命令失敗,第二個命令無法執行,但你不知道,因爲空捕獲吞噬了異常 – Steve