Transfer: SqlBulkCopy large class of data (more than ten thousand) inserted into a test

Transfer: https: //www.cnblogs.com/LenLi/p/3903641.html

Bo combined primary instance, test it myself, to 3 million lines of data more obvious! !

About mentioned on a blog, when inserted into the database during bulk data can be related to operating parameters Table by passing to a type of stored procedure, I did not test the efficiency of the process. Find later found to be a large data insertion through SqlBulkCopy class, this article will introduce the role of this class and do the relevant tests of efficiency (compared with the Insert statement).

SqlBulkCopy can only write data to SqlServer table, which can be either on the same server can be on a remote server. Of course, the data to be written that is not only SqlServer data source, the data source may be any, as long as the data can be loaded into a DataTable or DataReader read data.

Bulk insert data SqlBulkCopy class code is as follows:

1. First create a User class corresponding User table in the database.

Copy the code
 1 public class User
 2 {
 3     public User()
 4     {
 5         
 6     }
 7     public string UserName { get; set; }
 8     public string PassWord { get; set; }
 9     public string Address { get; set; }
10     public string Telephone { get; set; }
11 }
Copy the code

User database table structure:

2. Create a stored DataTable returned ten thousand methods of data:

Copy the code
 1     protected DataTable GetDataTableData()
 2     {
 3         List<User> userList=new List<User>();
 4         for (int i = 0; i < 10000; i++)
 5         {
 6             User user=new User();
 7             user.UserName = "zhangsan";
 8             user.PassWord = "123456";
 9             user.Address = "北京路一号";
10             user.Telephone = "13232323232";
11             userList.Add(user);
12         }
13         using (DataTable dt = new DataTable())
14         {
15             dt.Columns.Add("UserName", typeof (string));
16             dt.Columns.Add("PassWord", typeof(string));
17             dt.Columns.Add("Address", typeof(string));
18             dt.Columns.Add("Telephone", typeof(string));
19             foreach (User user in userList)
20             {
21                 DataRow dr = dt.NewRow();
22                 dr["UserName"] = user.UserName;
23                 dr["PassWord"] = user.PassWord;
24                 dr["Address"] = user.Address;
25                 dr["Telephone"] = user.Telephone;
26                 dt.Rows.Add(dr);
27             }
28             return dt;
29         }
30     }
Copy the code

3. Place a button on the page, you can click the button for data insertion operation, the button click event code is as follows:

Copy the code
Protected void the Button1_Click. 1 (SENDER Object, EventArgs E) 
 2 { 
 . 3 String Constr ConfigurationManager.ConnectionStrings = [ "Constr"] the ConnectionString;. 
 . 4 = the SqlConnection new new Connection the SqlConnection (Constr); 
 . 5 the try 
 . 6 { 
 . 7 the using (= the SqlBulkCopy new new bulkcopy the SqlBulkCopy (Constr, SqlBulkCopyOptions.KeepIdentity | SqlBulkCopyOptions.UseInternalTransaction)) 
 8 { 
 9 bulkCopy.DestinationTableName =. "dbo [the User]"; // target table, that you want to insert data into the table to which 
10 bulkCopy.ColumnMappings.Add ( "UserName", "UserName" ); // attribute mapping between the data source and the target table the column names 
11 bulkCopy.ColumnMappings.Add ( "PassWord", " PassWord"); 
12 bulkCopy.ColumnMappings.Add ( "Address", " Address"); 
13 is bulkCopy.ColumnMappings.Add ( "Telephone", "Telephone"); 
14 GetDataTableData the DataTable dt = (); // data source 
15 // bulkCopy. =. 3 the BatchSize; 
16 Stopwatch the Stopwatch the Stopwatch new new = (); // stopwatch, such statistics may be time 
17 stopwatch.Start (); // start the stopwatch 
18 bulkCopy.WriteToServer (dt); // data source writing data to the target table 
19 Response.Write ( "insert data used time:" + stopwatch.Elapsed); // end stopwatch, Elapsed time statistics to 
20 is} 
21 is} 
22 is the catch (Exception EX) 
23 is { 
24 throw new Exception (ex.Message);
25         }
26 Response.Write ( "<br/> successful insert !!!"); 
27}
Copy the code

SqlServer insert data in bulk by an Insert statement code is as follows:

1. First, create a stored procedure to insert ten thousand data, insert data and insert the data SqlBulkCopy exactly the same.

 

Copy the code
 1 CREATE PROC USP_InsertUserTable
 2 AS
 3 BEGIN
 4     DECLARE @i INT
 5     SET @i=1
 6     WHILE @i<10001
 7     BEGIN
 8         INSERT INTO dbo.[User]
 9                 ( UserName ,
10                   PassWord ,
11                   Address ,
12                   Telephone
13                 )
14         VALUES  ( N'zhangsan' , -- UserName - nvarchar(50)
15                   N'123456' , -- PassWord - nvarchar(6)
16                   N'北京路一号' , -- Address - nvarchar(50)
17                   '13232323232'  -- Telephone - varchar(11)
18                 )
19 
20                   SET @i=@i+1
21     END
22 END
Copy the code

 

2. Execute the stored procedure.
EXEC dbo.USP_InsertUserTable

SqlBulkCopy efficiency test comparison with the Insert statement is as follows:

SqlBulkCopy time spent Statistics (5 times):

SqlServer Insert statement has spent time statistics (5), through the tool - "Sql Server Profiler to test the template using TSQL_Duration:

One thing to note: Insert statement or whether SqlBulkCopy operate, should be done to clean up the buffer before each test and delete the original data, so as not to affect the conduct of the test, as follows:

DBCC DROPCLEANBUFFERS
DBCC FREESYSTEMCACHE( 'ALL' ) 
DELETE dbo.[User]

 

Conclusion: The above test statistical analysis, it can be seen SqlBulkCopy large data insertion operation performance efficiency is significantly more efficient than Insert statement.

Guess you like

Origin www.cnblogs.com/newcapecjmc/p/10948154.html