Use sqlbulkcopy to migrate data in Asp.net 2.0

Source: Internet
Author: User
Tags connectionstrings
We often need to migrate data from one table to another. Of course, there are many methods used. In. NET 2.0, A sqlbulkcopy class is provided, and the following operations can be implemented. For example, a table is as follows:
Create Table Person3
(

Personid int identity (1, 1) primary key,
Name nvarchar (200 ),
E-mail nvarchar (200 ),
Picture Image

)

Insert into person3 (name, email, picture)
Select name, email, picture from person

If the person table already exists, the preceding statement can insert data to the person3 table (executed in SQL Server 2005 ). Now we use the followingCodeTo achieve
String connectionstring = configurationmanager. connectionstrings ["mydatabase"]. connectionstring;

Sqlconnection myconnection = new sqlconnection (connectionstring );

Sqlcommand mycommand = new sqlcommand ("select * From person", myconnection );

Myconnection. open ();

Sqldatareader DR = mycommand. executereader ();

Sqlconnection mynewconnection = new sqlconnection (connectionstring );

Mynewconnection. open ();

Sqlbulkcopy bulk = new sqlbulkcopy (mynewconnection );

Bulk. destinationtablename = "[person3]";

Try

{

Bulk. writetoserver (DR );

}

Catch (exception ex)

{

Response. Write (ex. Message );

}

Finally

{

Mynewconnection. Close ();

Dr. Close ();

Myconnection. Close ();

Bulk. Close ();

}

Next, we will parse it. First, create a new database connection, and then use the classic code. extract the data from the person table and use it in datareader. Then, we create a new data connection.

Sqlbulkcopy bulk = new sqlbulkcopy (mynewconnection );

Bulk. destinationtablename = "[person3]";

Upload mynewconnection as a parameter to the constructor of the sqlbulkcopy class, and specify the target table to be migrated as person3.
Then use bulk. writetoserver (DR); to migrate the data.
The structure of the above person table and person3 is identical. What should I do if the structure is different? The following example shows how to create a table person2.
Create TablePerson2
(
Personid int identity (1, 1) primary key,
Firstname nvarchar (200 ),
Lastname nvarchar (200 ),

E-mail nvarchar (200 ),
Picture Image
)
If we migrate the person table to the person2 table above, an error will occur. Because the fields are different, we will use the following code
String connectionstring = configurationmanager. connectionstrings ["mydatabase"]. connectionstring;

Sqlconnection myconnection = new sqlconnection (connectionstring );

Sqlcommand mycommand = new sqlcommand ("select * From person", myconnection );

Myconnection. open ();

Sqldatareader DR = mycommand. executereader ();

Sqlconnection mynewconnection = new sqlconnection (connectionstring );

Mynewconnection. open ();

Sqlbulkcopy bulk = new sqlbulkcopy (mynewconnection );

Bulk. destinationtablename = "[person2]";

Bulk. columnmappings. Add ("Name", "lastname");

Bulk. columnmappings. Add ("Email", "email");

Bulk. columnmappings. Add ("Picture", "picture");

Try

{

Bulk. writetoserver (DR );

}

Catch (exception ex)

{

Response. Write (ex. Message );

}

Finally

{

Mynewconnection. Close ();

Dr. Close ();

Myconnection. Close ();

Bulk. Close ();

}

As you can see, here we useBulk. columnmappings. Add ("...", "...") The source target field matches the field of the target table.
It is said that the sqlbulkcopy class is used, and the performance is very good in the case of a large amount of data.


 

Related Article

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.