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.