批处理DataTable

时间:2022-07-27 08:12:14

DataTable dt = CreateTable();

SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True");

SqlCommand myCommand = new SqlCommand("update sqlcommand set sname='@sname' where sid=@sid  ", (SqlConnection)conn);

SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);

SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);

myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand();

try             {

lock (this)                              //处理并发情况(分布式情况

{                     conn.Open();

myAdapter.Update(dt);

conn.Close();

}

}

catch(Exception se)

{

Response.Write(se.Message);

}

public DataTable CreateTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("sname");
            dt.Columns.Add("sid");
            DataRow dr = dt.NewRow();
            dr["sname"] = "tx";
            dr["sid"] = "1";
            dt.Rows.Add(dr);
            DataRow drs = dt.NewRow();
            drs["sname"] = "John";
            drs["sid"] = "3";
            dt.Rows.Add(drs);
            return dt;
        }