Batch Update data

Source: Internet
Author: User
The data was recently done in a project that encountered batch update data, the code is as follows:

An example of an update:
<%@ Page language= "C #" debug= "true"%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SqlClient"%>

<script runat= "Server" >
void Page_Load (object sender, System.EventArgs e) {
SqlConnection conn = new SqlConnection ("server=localhost;uid=sa;pwd=kingapex;database=tourmsg");
SqlDataAdapter da = new SqlDataAdapter ();
SqlCommand cmd = new SqlCommand ("Select top 2 Id,groupnumber as Groupnumber1 from [GROUPTB]", conn);
DataSet ds = new DataSet ();
Conn. Open ();
Da. SelectCommand = cmd;
Da. Fill (ds, "GROUPTB");


for (int i=0;i<ds. tables["GROUPTB"]. rows.count;i++)
{

Response.Write (ds. tables["GROUPTB"]. rows[1]+ "<br>");
}


for (int i=0;i<ds. tables["GROUPTB"]. rows.count;i++)
{
Ds. tables["GROUPTB"]. Rows.beginedit ();
Ds. tables["GROUPTB"]. ROWS[1] = "WANGFENGRR";
Ds. tables["GROUPTB"]. Rows.endedit ();
}

String updatecmd = "Update grouptb set groupnumber = @groupnumber Where id= @id";


cmd = new SqlCommand (UPDATECMD, conn);
Cmd. Parameters.Add ("@id", sqldbtype.int,4, "id");
Cmd. Parameters.Add ("@groupnumber", sqldbtype.char,10, "Groupnumber1");

Da. UpdateCommand = cmd;
Da. Update (ds, "GROUPTB");
Ds. AcceptChanges ();

Conn. Close ();


}
</script>

Here is an example of an insert:

<%@ Page language= "C #" debug= "true"%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SqlClient"%>

<script runat= "Server" >
void Page_Load (object sender, System.EventArgs e) {

Establishing a DataTable data source
DataTable Dt = new DataTable ();

DataRow Dr;

DT.COLUMNS.ADD (New DataColumn ("Groupnumber"));



for (int J =0;j<3;j++) {

Dr=dt.newrow ();

dr[0]= "Groupnumber" +j.tostring ();

DT.ROWS.ADD (DR);

}


SqlConnection conn = new SqlConnection ("server=localhost;uid=sa;pwd=kingapex;database=tourmsg");


SqlDataAdapter myadapter = new SqlDataAdapter ();

Establish InsertCommand
StringBuilder sb = new StringBuilder ("");
Sb. Append ("INSERT GROUPTB (Groupnumber) VALUES (");
Sb. Append ("@groupnumber)");
Myadapter.insertcommand = new SqlCommand ();
MyAdapter.InsertCommand.CommandText = sb. ToString ();
MyAdapter.InsertCommand.Connection = conn;


SqlParameter p1 = new SqlParameter ("@groupnumber", SqlDbType.VarChar, 40);
P1. SourceVersion = DataRowVersion.Current;
P1. SourceColumn = "Groupnumber"; or P1. SourceColumn = Dt.columns[0]. ColumnName;
MyAdapter.InsertCommand.Parameters.Add (p1);

Update
Myadapter.update (Dt);

Conn. Close ();
}
</script>





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.