Batch insert data in ASP. NET

Source: Internet
Author: User

To insert records in batches, you can use sqldataadapter.Example:

String SQL =   " Select Item1, item2, item3 from tablea " ;
Sqlconnection CN = New Sqlconnection ( " Server = (local); uid = sa; Pwd =; database = examonline " );
Sqldataadapter da = New Sqldataadapter (SQL, CN );
Dataset DS = New Dataset ();
Try
{
CN. open ();
Da. Fill (DS,"Dstable1");
}
Finally
{
CN. Close ();
}  

// Append the batch data to DS
Checkbox CB = New Checkbox ();
Datarow Dr;
For ( Int I = 0 ; I < Datagrid1.items. Count; I ++ )
{
CB = (Checkbox) maid [I]. findcontrol ( " Checkbox1 " );
If (Cb. Checked = True )
{
Dr = DS. Tables [ " Dstable1 " ]. Newrow ();
Dr [ " Item1 " ] = (Label) maid [I]. findcontrol ( " Label1 " ). Text;
Dr [ " Item2 " ] = (Label) maid [I]. findcontrol ( " Label2 " ). Text;
Dr [ " Item3 " ] = (Label) maid [I]. findcontrol ( " Label3 " ). Text;
DS. Tables [ " Dstable1 " ]. Rows. Add (DR );
}
}

// Preparations before adding data in batches
SQL = " Insert into tablea (Item1, item2, item3) values (@ Item1, @ item2, @ item3) " ;
Da. insertcommand = New Sqlcommand (SQL, CN );
Sqlparameter Param = New Sqlparameter ();
Param = Da. insertcommand. Parameters. Add ( New Sqlparameter ( " @ Item1 " , Sqldbtype. Char, 50 ));
Param. sourceversion = Datarowversion. Current;
Param. sourcecolumn =   " Item1 " ;

Param = Da. insertcommand. Parameters. Add ( New Sqlparameter ( " @ Item2 " , Sqldbtype. Char, 50 ));
Param. sourceversion = Datarowversion. Current;
Param. sourcecolumn =   " Item2 " ;

Param = Da. insertcommand. Parameters. Add ( New Sqlparameter ( " @ Item3 " , Sqldbtype. Char, 50 ));
Param. sourceversion = Datarowversion. Current;
Param. sourcecolumn =   " Item3 " ;

// Batch add data
Try
{
CN. open ();
Da1.update (DS,"Dstable1");
}
Catch (Exception ex)
{
Label1.text="Database Error:" +Ex. Message. tostring ();
}
Finally
{
CN. Close ();
}


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.