#region 使用SqlBulkCopy將DataTable中的數據批量插入數據庫中 /// <summary> /// 注意:DataTable中的列須要與數據庫表中的列徹底一致。/// </summary> /// <param name="conStr">數據庫鏈接串</param> /// <param name="strTableName">數據庫中對應的表名</param> /// <param name="dtData">數據集</param> public static void SqlBulkCopyInsert(string conStr, string strTableName, DataTable dtData) { try { using (SqlBulkCopy sqlRevdBulkCopy = new SqlBulkCopy(conStr)) //引用SqlBulkCopy { sqlRevdBulkCopy.DestinationTableName = strTableName; //數據庫中對應的表名 sqlRevdBulkCopy.NotifyAfter = dtData.Rows.Count; //有幾行數據 sqlRevdBulkCopy.WriteToServer(dtData); //數據導入數據庫 sqlRevdBulkCopy.Close(); //關閉鏈接 } } catch (Exception ex) { throw (ex); } } #endregion