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