/// <summary>
/// 使用SqlDataAdapter适配器修改数据库的数据操作
/// </summary>
/// <param name="sender">这是基于Datable填充数据</param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
string name = TextBox1.Text;
string id = TextBox2.Text;
//首先是把要修改这张表的数据取出来
SqlDataAdapter sdat = new SqlDataAdapter("select id,name from xx", sh.GetConnction());
DataTable dt = new DataTable();
//用适配器对象fill方法填充到DataTable对象当
sdat.Fill(dt);
//然后再用适配器对象updatecommand属性更新数据源当中的记录
sdat.UpdateCommand = new SqlCommand("update xx set name ='" + name + "' where id='" + id + "'",sh.GetConnction());
DataRow row = dt.Rows[0];
row["name"] = name;
if(sdat.Update(dt)>0)
Response.Write("<script>alert('修改成功')</script>");
else
Response.Write("<script>alert('修改失败')</script>");
}
/// <summary>
/// 使用SqlDataAdapter适配器修改数据库的数据操作
/// </summary>
/// <param name="sender">基于DataSet</param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
string name = TextBox1.Text;
string id = TextBox2.Text;
//首先是把要修改这张表的数据取出来
SqlDataAdapter sdat = new SqlDataAdapter("select id,name from category", sh.GetConnction());
DataSet dt = new DataSet();
//用适配器对象fill方法填充到DataSet对象当
sdat.Fill(dt,"xx");
//然后再用适配器对象updatecommand属性更新数据源当中的记录
sdat.UpdateCommand = new SqlCommand("update xx set name ='" + name + "' where id='" + id + "'",sh.GetConnction());
dt.Tables["xx"].Rows[0]["name"] = "id";
if(sdat.Update(dt.Tables[0])>0)
Response.Write("<script>alert('修改成功')</script>");
else
Response.Write("<script>alert('修改失败')</script>");
}
[color=red]我自己总结的两种方法!! 太晚了洗洗睡吧 ^_^[/color]