2013-03-25 54 views
0

先生/女士,C#DateTime更改爲另一種格式

我將執行Oracle批量插入,但無效的綁定參數參數:System.dateTime在此處發出警告。

創建日期的字段是時間戳(0),其中22-MAR-13 08.13.27.000000000 PM僅爲accpeted格式。

但是當我試圖從字符串轉換爲DateTime如下:

2013年3月22日下午八時00分00秒

使用下面的方法:

item.CreatedDate = Convert.ToDateTime("19-MAR-13 08.13.27 PM"); 

// BELOW IS ORACLE BULK INSERT

 using (OracleConnection myConnection = new OracleConnection(myConnectionString)) 
     { 
      myConnection.Open(); 
      using (var copy = new OracleBulkCopy(myConnection)) 
      { 
       copy.DestinationTableName = "T_BQ"; 
       copy.BulkCopyTimeout = 10; 
       copy.BatchSize = 1000; 
       var query = from item in list select item; 
       var dt = new System.Data.DataTable(); 
       dt = ConvertToDataTable(query); 
       copy.WriteToServer(dt); 
       copy.Dispose(); 
       copy.Close(); 
      } 
      myConnection.Dispose(); 
      myConnection.Close(); 
     } 
+0

你檢查我的答案? – Arshad 2013-03-25 07:14:11

+0

檢查,它的工作原理! – 2013-03-26 07:02:00

回答

3

您可以使用DateTime.TryParseExact定製datetime格式爲:

string strDateStarted = "19-MAR-13 08.13.27 AM"; 
DateTime datDateStarted; 
DateTime.TryParseExact(strDateStarted, new string[] { "dd-MMM-yy hh.mm.ss tt" }, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out datDateStarted); 
Console.WriteLine(datDateStarted); 
3

您應該使用:進行時間分隔用r .

試試這個:

item.CreatedDate = Convert.ToDateTime("19-MAR-13 08:13:27 PM"); 
+0

無效的參數綁定 參數名稱:System.DateTime 發生 – 2013-03-25 03:16:01

+0

@RjujuGujarati:在您的問題中發佈更多代碼,尤其是導致錯誤的代碼。 – 2013-03-25 03:18:21

+0

copy.WriteToServer(dt);錯誤 – 2013-03-25 03:39:04

2

試試這個,

dateVariable.ToString("MM/dd/yyyy hh:mm:sstt");