Database inserts 10w data at once, how to insert efficiency fast

Source: Internet
Author: User
Tags bulk insert

    1. Inserting a piece of data into SQL Server uses the INSERT statement, but if you want to bulk insert a bunch of data, looping through insert is inefficient and results in a system performance problem with SQL

    2. The following are the two bulk data insertion methods supported by SQL Server: Bulk and table-valued parameters (table-valued Parameters).

    3. The main idea of the bulk method is to insert the data in the table into the database by sqlbulkcopy the data in the table at the client, and then using the

    4. The code is as follows:

    5. public static void Bulktodb (DataTable dt)
      {
      SqlConnection sqlconn = new SqlConnection (
      configurationmanager.connectionstrings["ConnStr"]. ConnectionString);
      SqlBulkCopy bulkcopy = new SqlBulkCopy (sqlconn);
      Bulkcopy.destinationtablename = "Bulktesttable";
      bulkcopy.batchsize = dt. Rows.Count;
      Try
      {
      Sqlconn.open ();
      if (dt! = null && dt. Rows.Count! = 0)
      Bulkcopy.writetoserver (DT);
      }
      catch (Exception ex)
      {
      Throw ex;
      }
      Finally
      {
      Sqlconn.close ();
      if (bulkcopy! = null)
      Bulkcopy.close ();
      }
      }
      public static DataTable GetTableSchema ()
      {
      DataTable dt = new DataTable ();
      Dt. Columns.addrange (New datacolumn[]{
      New DataColumn ("Id", typeof (int)),
      New DataColumn ("UserName", typeof (String)),
      New DataColumn ("Pwd", typeof (String))});

      return DT;
      }
      static void Main (string[] args)
      {
      Stopwatch SW = new Stopwatch ();
      for (int multiply = 0; multiply < multiply++)
      {
      DataTable dt = Bulk.gettableschema ();
      for (int count = multiply * 100000; count < (multiply + 1) * 100000; count++)
      {
      DataRow r = dt. NewRow ();
      R[0] = count;
      R[1] = string. Format ("user-{0}", Count * multiply);
      R[2] = string. Format ("pwd-{0}", Count * multiply);
      Dt. Rows.Add (R);
      }
      Sw. Start ();
      BULK.BULKTODB (DT);
      Sw. Stop ();
      Console.WriteLine (String. Format ("Elapsed time is {0} Milliseconds", SW. Elapsedmilliseconds));
      }
      Console.ReadLine ();
      }

    6. Reprinted from: http://www.aspnetjia.com/Cont-133.html

Database inserts 10w data at once, how to insert efficiency fast

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.