SqlBulkCopy 数据批量操作使用的类
private void SqlBulkCopyByDataTable(string connectionString,string TableName,DataTable dt) { using (SqlConnection sqlconn = new SqlConnection("connectionstring")) { using (SqlBulkCopy sqlbulkcopy = new SqlBulkCopy("connectionstring",SqlBulkCopyOptions.UseInternalTransaction)) { try { sqlbulkcopy.DestinationTableName = TableName; for(int i = 0;i<dt.Columns.Count;i++) { sqlbulkcopy.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName);//第一个参数数据源中源列名称,列二个参数目标表列名 } sqlbulkcopy.WriteToServer(dt); } catch(System.Exception ex) { throw ex; } } } }