C # Bulk update of SQL instances

Source: Internet
Author: User

The example of this article describes the C # batch update SQL method, share to everyone for your reference. Here's how:

To implement bulk update card data, the following are the main steps:

1, first establish a database connection

2. Populate the DataSet with part of the data

3. Modify the values of the data in the dataset

4. Update the DataSet

5, the operation of the cycle, the specific operation process see the following code:


The code is as follows:

public void BatchUpdate (list<card> List)

{

using (SqlConnection conn = new SqlConnection (dbhelpersql.connectionstring))

{

Conn. Open ();

using (SqlDataAdapter da = New SqlDataAdapter ())

{

Da. SelectCommand = new SqlCommand ("Select Top Scanflag,cardid from Card", conn);

DataSet ds = new DataSet ();

Da. Fill (DS);

Da. UpdateCommand = new SqlCommand ("update Card set scanflag = @ScanFlag where CardId = @CardId", conn);

Da. UPDATECOMMAND.PARAMETERS.ADD ("@ScanFlag", Sqldbtype.bit, 1, "Scanflag");

Da. UPDATECOMMAND.PARAMETERS.ADD ("@CardId", SqlDbType.Int, 4, "CardId");

Da. Updatecommand.updatedrowsource = Updaterowsource.none;

Da. updatebatchsize = 0;

for (int i = 0; i < list. Count; i++)

{

for (int j = 0; J < ds. Tables[0]. Rows.Count; J + +, i++)

{

Ds. Tables[0]. ROWS[J]. BeginEdit ();

Ds. Tables[0]. rows[j]["Scanflag"] = true;

Ds. Tables[0]. rows[j]["CardId"] = List[i]. CardId;

Ds. Tables[0]. ROWS[J]. EndEdit ();

if (i = = list. COUNT–1)

Break

}

Da. Update (ds. Tables[0]);

}

Ds. Clear ();

Ds. Dispose ();

}

}

}

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Bulk update of SQL instances

This address: http://www.paobuke.com/develop/c-develop/pbk23435.html






Related Content C # ListView Click the column header sort Instance C # Send mailbox implementation code C # asynchronous download file easy to learn C # boxing and unboxing
C # Understanding lambda expressions in C # by streaming write data to a file WinForm exporting Dataviewgrid data to Excel Method C # WinForm Control naming conventions

C # Bulk update of SQL instances

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.