Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report