Get the App
SLTechnology News&Howtos  ›  Database  › 

How to insert data into SQL Server using SqlBulkCopy in asp.net

Shulou Source: shulou.com Published: 2022-05-31 19:25:40 09月19日 Update

How to use SqlBulkCopy in asp.net to insert data into SQL Server, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

The code is as follows: using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; using Microsoft.ApplicationBlocks.Data; namespace ConsoleAppInsertTest {class Program {static int count = 1000000; / / the number of entries inserted static void Main (string [] args) {long sqlBulkCopyInsertRunTime = SqlBulkCopyInsert (); Console.WriteLine ("the time it takes to insert {1} pieces of data using SqlBulkCopy is {0} milliseconds", sqlBulkCopyInsertRunTime, count); long commonInsertRunTime = CommonInsert () Console.WriteLine (string.Format ("normal time to insert {1} pieces of data is {0} milliseconds", commonInsertRunTime, count); Console.ReadKey (); / use normal insert data / private static long CommonInsert () {Stopwatch stopwatch = new Stopwatch (); stopwatch.Start (); for (int I = 0; I < count) ) {SqlHelper.ExecuteNonQuery (SqlHelper.SqlConnection, CommandType.Text, "insert into passport (PassportKey) values ('" + Guid.NewGuid () + "));} stopwatch.Stop (); return stopwatch.ElapsedMilliseconds;} / insert data / private static long SqlBulkCopyInsert () {Stopwatch stopwatch = new Stopwatch (); stopwatch.Start (); DataTable dataTable = GetTableSchema (); for (int I = 0; I < count) ITunes +) {DataRow dataRow = dataTable.NewRow (); dataRow [2] = Guid.NewGuid (); dataTable.Rows.Add (dataRow);} / Console.WriteLine (stopwatch.ElapsedMilliseconds); / / initialize data time SqlBulkCopy sqlBulkCopy = new SqlBulkCopy (SqlHelper.SqlConnection); sqlBulkCopy.DestinationTableName = "Passport"; if (dataTable! = null & & dataTable.Rows.Count! = 0) {sqlBulkCopy.WriteToServer (dataTable);} sqlBulkCopy.Close (); stopwatch.Stop (); return stopwatch.ElapsedMilliseconds } private static DataTable GetTableSchema () {return SqlHelper.ExecuteDataset (SqlHelper.SqlConnection, CommandType.Text, "select * from Passport where 1: 2") .Tables [0];}

This is the answer to the question about how to insert data into SQL Server using SqlBulkCopy in asp.net. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Tags: Data time question ordinary used way more help answer easy easy code content guy partner method knowledge article industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno macOS Shulou Technology Linux Apple