SqlBulkCopy(批量复制)使用方法 && SqlDataAdapter Update
SqlBulkCopy提供了一种将数据复制到Sql Server数据库表中高性能的方法。SqlBulkCopy 包含一个方法 WriteToServer,它用来从数据的源复制数据到数据的目的地。 WriteToServer方法可以处理的数据类型有DataRow[]数组,DataTable 和 DataReader。 你可以根据不同的情形使用不同的数据类型。SqlBulkCopy其原理是采用了SQL Server的BCP协议进行数据的批量复制。
SqlBulkCopy(批量复制):批量复制速度极快
/// <summary> /// 数据库中的表复制到另一个数据库中的表 /// </summary> private static void SqlBulkCopyMethod(string connectionStr1, string connectionStr2) { try { using (SqlConnection connetionPub = new SqlConnection(connectionStr1)) { SqlCommand commandPub = connetionPub.CreateCommand(); using (commandPub) { commandPub.CommandText = "select top 100 * from Person"; commandPub.CommandType = System.Data.CommandType.Text; connetionPub.Open(); SqlConnection connectionBulkCopy = new SqlConnection(connectionStr2); using (connectionBulkCopy) { connectionBulkCopy.Open(); SqlDataReader dataReader = commandPub.ExecuteReader(); SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionBulkCopy); using (bulkCopy) { bulkCopy.BulkCopyTimeout = 3600; bulkCopy.BatchSize = 300000;//BatchSize的意思就是同每一批次中的行数,在每一批次结束时,就将该批次中的行发送到数据库 bulkCopy.NotifyAfter = 1; bulkCopy.SqlRowsCopied += new SqlRowsCopiedEventHandler(bulkCopy_SqlRowsCopied);//bulkCopy对象提供了一个SqlRowCopied事件,在每次处理完NotifyAfter属性指定的行数时发生 bulkCopy.DestinationTableName = "Person"; bulkCopy.WriteToServer(dataReader); } } } } } catch (Exception ex) { throw ex; } } public static void bulkCopy_SqlRowsCopied(object obj, SqlRowsCopiedEventArgs e) { //执行事件处理方法 }
/// <summary> /// 将表中资料批量插入到数据库 /// </summary> /// <param name="connectionString"></param> /// <param name="TableName"></param> /// <param name="dt"></param> private void SqlBulkCopyByDatatable(string connectionString, DataTable dt) { using (SqlConnection conn = new SqlConnection(connectionString)) { using (SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction)) { try { sqlbulkcopy.DestinationTableName = "Person"; 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; } } } }
/// <summary> /// 不同表之间复制数据 /// </summary> private static void PerformBulkCopyDifferentSchema(string connectionString) { var stopwatch = new Stopwatch(); DataTable sourceData = new DataTable(); // 源 stopwatch.Start(); using (SqlConnection sourceConnection = new SqlConnection(connectionString)) { SqlCommand myCommand = new SqlCommand("SELECT * FROM Person", sourceConnection); sourceConnection.Open(); SqlDataReader reader = myCommand.ExecuteReader(); // 目的 using (SqlConnection destinationConnection = new SqlConnection(connectionString)) { // 打开连接 destinationConnection.Open(); using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString)) { bulkCopy.ColumnMappings.Add("PersonId", "NEW_PersonId"); bulkCopy.ColumnMappings.Add("Name", "NEW_Name"); bulkCopy.ColumnMappings.Add("Age", "NEW_Age"); bulkCopy.DestinationTableName = "New_Person"; bulkCopy.WriteToServer(reader); } } Console.WriteLine(stopwatch.ElapsedMilliseconds); reader.Close(); } }
/// <summary> /// 使用XML作为数据源 /// </summary> private static void PerformBulkCopyXMLDataSource(string connectionString) { DataSet ds = new DataSet(); DataTable sourceData = new DataTable(); ds.ReadXml(@"E:\Study\SQL\MSSQL\MSSQL_SqlBulkCopy\MSSQL_SqlBulkCopy\PersonFile.xml"); sourceData = ds.Tables[0]; // 目的 using (SqlConnection destinationConnection = new SqlConnection(connectionString)) { // 打开连接 destinationConnection.Open(); using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString)) { // 列映射 //bulkCopy.ColumnMappings.Add("PersonId", "NEW_PersonId"); //bulkCopy.ColumnMappings.Add("Name", "NEW_Name"); //bulkCopy.ColumnMappings.Add("Age", "NEW_Age"); bulkCopy.DestinationTableName = "New_Person"; bulkCopy.WriteToServer(sourceData); } } }
SqlDataAdapter Update:适用于批量更新数据
/// <summary> /// SqlDataAdapter Update /// </summary> private static void PerformSqlDataAdapterUpdate(string connectionString) { using (SqlConnection conn = new SqlConnection(connectionString)) { DataSet ds = new DataSet(); DataSet ds_new = new DataSet(); string SQLstr = "Select top 100 * from Person"; string SQLstr_new = "Select top 0 * from New_Person"; conn.Open(); SqlCommand comm = new SqlCommand(SQLstr_new, conn); SqlDataAdapter sad = new SqlDataAdapter(comm); SqlCommandBuilder scb = new SqlCommandBuilder(sad); sad.Fill(ds_new); SqlDataAdapter sda = new SqlDataAdapter(new SqlCommand(SQLstr, conn)); sda.Fill(ds); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { DataRow NewRow = ds_new.Tables[0].NewRow(); NewRow[0] = ds.Tables[0].Rows[i][0]; NewRow[1] = ds.Tables[0].Rows[i][1]; NewRow[2] = ds.Tables[0].Rows[i][2]; ds_new.Tables[0].Rows.Add(NewRow); } // ds_new.AcceptChanges(); sad.Update(ds_new.GetChanges()); } }
代码:https://files.cnblogs.com/files/imhaiyang/MSSQL_SqlBulkCopy.rar
转载:http://www.cnblogs.com/scottckt/archive/2011/02/16/1955862.html
声明:本文纯属个人随手笔记,如果对您有参考价值我十分开心,如果有存在错误,或者有更好的解决办法也麻烦您留言告诉我,大家共同成长,切勿恶言相。
欢迎加入MSDN技术交流群:235937854,一起发现知识、了解知识、学习知识、分享知识