SqlCommandBuilder 小示例
protected void Button5_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Server=.;Database=test;Integrated Security=SSPI");
conn.Open();
//填充DateSet
SqlDataAdapter da = new SqlDataAdapter("select * from Test",conn );
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "Test");
//在使用Dateset之前我们必须ds.fill来填充内存数据库...
//添加数据...
DataRow dr = ds.Tables["Test"].NewRow();
dr["StuId"]="444";
dr["StuName"] = "中国";
dr["Sutscore"] = 444;
ds.Tables["Test"].Rows.Add(dr);
try
{
da.Update(ds, "Test");
Response.Write("数据已经添加");
}
catch
{
Response.Write("出现异常 ");
}
conn.Close();
}