轉自:http://www.maomao365.com/?p=5613
摘要:
下文講述使用c#程式碼快速將dataTable匯入至mssql資料庫的方法
實現思路:
需要將datatable調整為同目標表,表結構一致,然後採用
SqlBulkCopy 中快速writeToServer方法,可以快速將datatable複製至目標表中,
具體程式碼如下所示:
#region DatatTable快速寫入 ///tb 待匯入表 ///tbName mssql資料庫中目標表表名 public string dataTableInsert(DataTable tb, string tbName) { string str = "0"; try { DbHelper d = new DbHelper(); SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(d.Conn.ConnectionString.ToString(), SqlBulkCopyOptions.UseInternalTransaction); sqlbulkcopy.DestinationTableName = tbName; sqlbulkcopy.WriteToServer(tb); str = "1"; d.Dispose(); d.Close(); } catch (Exception e) { str = e.Message.ToString(); } return str; } #endregion