ADO BULK INSERT

Source: Internet
Author: User
Tags rowcount

In. Net1.1, it is not convenient whether to bulk insert all the data in the entire DataTable into the database, or to migrate between different data sources. In. Net2.0, several new classes are added under the SqlClient namespace to help us migrate data in a DataTable or DataReader batch. The data source can come from a relational database or an XML file, or even webservice return results. One of the most important classes is the SqlBulkCopy class, which can be used to help us migrate data from the source to the target database conveniently.

1   Public classQueue2     {3          PublicQueue (stringContent, Queuetype type,stringofficenumber)4         {5Content =content;6Type =type;7Officenumber =Officenumber;8         }9 Ten          PublicQueue (DateTime Proccesstime,intInternalnumber,stringContent, Queuetype type,stringofficenumber) One         { AProcesstime =Proccesstime; -Internalnumber =Internalnumber; -Content =content; theType =type; -Officenumber =Officenumber; -         } -  +         /// <summary> -         ///Processing Time +         /// </summary> A          PublicDateTime Processtime {Get;Set; } at         /// <summary> -         ///Internal number -         /// </summary> -          Public intInternalnumber {Get;Set; } -         /// <summary> -         ///content in         /// </summary> -          Public stringContent {Get;Set; } to         /// <summary> +         ///Mailbox Type -         /// </summary> the          PublicQueuetype Type {Get;Set; } *         /// <summary> $         ///Agent NumberPanax Notoginseng         /// </summary> -          Public stringOfficenumber {Get;Set; } the  +         /// <summary> A         ///determine whether the given content string is a valid letter content; the         /// </summary> +         /// <param name= "queuecontent" ></param> -         /// <param name= "Officnumber" > </param> $          Public Static BOOLValidate (stringQueuecontent,stringofficnumber) $         { -             returnQueuecontent.contains (officnumber); -         } the}
Entity
1         PrivateDataTable GetTableSchema ()2         {3             varDataTable =NewDataTable ();4DataTable.Columns.AddRange (New[]5                                            {6                                                NewDataColumn ("Processtime"),7                                                NewDataColumn ("Internalnumber"),8                                                NewDataColumn ("Content"),9                                                NewDataColumn ("Type"),Ten                                                NewDataColumn ("Officenumber"), One                                            }); A             returndataTable; -}
Create a DataTable
1          Public intADD (system.collections.generic.list<domain.queue>queues)2         {3             intRowCount =0;4             varDataTable =GetTableSchema ();5 6             foreach(varQueueinchqueues)7             {8DataRow datarow =Datatable.newrow ();9datarow[0] =queue. Processtime;Tendatarow[1] =queue. Internalnumber; Onedatarow[2] =queue. Content; Adatarow[3] =queue. Type; -datarow[4] =queue. Officenumber; - DataTable.Rows.Add (dataRow); the             } -              -             using(varSqlConnection =NewSqlConnection (ConnectionString)) -             { + Sqlconnection.open (); -                 using(varSqlBulkCopy =NewSqlBulkCopy (sqlConnection)) +                 { ASqlbulkcopy.destinationtablename ="history.queues"; atSqlbulkcopy.batchsize =DataTable.Rows.Count; -                     if(Sqlbulkcopy.batchsize! =0) -                     { - Sqlbulkcopy.writetoserver (dataTable); -RowCount =sqlbulkcopy.batchsize; -                     } in                 } -             } to  +             returnRowCount; -}
Bulk Operations

Code Analysis:

var sqlBulkCopy = new SqlBulkCopy (sqlConnection)

Mr. SqlBulkCopy as an instance, the constructor specifies the target database and uses the SqlConnection link to create

Sqlbulkcopy.writetoserver (dataTable);

rowCount = sqlbulkcopy.batchsize;

RowCount property specifies the number of rows of data to be processed before notification notification events

The WriteToServer method is to copy the data source to the target database. Before you can use the WriteToServer method, you must first specify the DestinationTableName property, which is the table name of the target database.

ADO BULK INSERT

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.