asp.net (C #) DataTable Fast Import Database

Source: Internet
Author: User

The

DataTable represents a memory-related datasheet that can be created and used using control drag-and-drop in the toolbar, or created and used independently as needed in the programming process, most commonly as a member of a dataset. In this case, you need to create the data table dynamically as needed during the programming process

public void inserttable (DataTable dt, String tabelname, DataColumnCollection dtcolum)
    {
& nbsp;     
       //declaring database connections
         Mycon. Open ();
       //declaring SqlBulkCopy, using to release unmanaged Resources
         using (sqlbulkcopy SQLBC = new SqlBulkCopy (mycon))
        {
& nbsp;          //One-time BULK insert data volume
             sqlbc.batchsize = 1000;
The number of seconds allowed to complete the operation before the            //timeout, if the transaction is not committed and the data is rolled back, All replicated rows will be removed from the target table
            sqlbc.bulkcopytimeout = 60 ;

The Notifyafter property to call the corresponding event each time you insert 10,000 data.
Sqlbc.notifyafter = 10000;
Sqlbc.sqlrowscopied + = new Sqlrowscopiedeventhandler (onsqlrowscopied);
Set up tables to be written in bulk
Sqlbc.destinationtablename = Tabelname;
The custom DataTable corresponds to the fields of the database
SQLBC.COLUMNMAPPINGS.ADD ("id", "tel");
SQLBC.COLUMNMAPPINGS.ADD ("name", "Neirong");
for (int i = 0; i < Dtcolum.count; i++)
{
SQLBC.COLUMNMAPPINGS.ADD (Dtcolum[i]. Columnname.tostring (), Dtcolum[i]. Columnname.tostring ());
}
Bulk Write
Sqlbc.writetoserver (DT);
}
Mycon. Dispose ();
}
event when responding
void Onsqlrowscopied (object sender, Sqlrowscopiedeventargs e)
{
Response.Write ("<br/> ok!");
}
Dataadapter.selectcommand is used to obtain data, so its CommandText should be a SELECT statement;
Dataadapter.insertcommand is used to insert data, so its CommandText should be an INSERT statement;
Dataadapter.deletecommand is used to delete data, so its CommandText should be the DELETE statement;
Dataadapter.updatecommand is used to update the data, so its CommandText should be the UPDATE statement;

So your code should write this:
Selectsql= "SELECT * from Tblname";
SqlCommand selectcmd = new SqlCommand (Selectsql, Con);

Insertsql= "INSERT into Tblname (input_time,location_id) VALUES (@Input_Time, @Location_ID)";

SqlCommand insertcmd = new SqlCommand (Insertsql, Con);
INSERTCMD.PARAMETERS.ADD (New System.Data.SqlClient.SqlParameter ("@Input_Time", System.Data.SqlDbType.Time));
INSERTCMD.PARAMETERS.ADD (New System.Data.SqlClient.SqlParameter ("@Location_ID", System.Data.SqlDbType.NVarChar)) ;

SqlDataAdapter Dad = new SqlDataAdapter ();
Dad.selectcommand = Selectcmd;
Dad.insertcommand = Insertcmd;
Dad.update (DT);

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.