修改
string t;
SqlConnection conn=new SqlConnection();
int empID = (int)DataList1.DataKeys[e.Item.ItemIndex];
//取得文本框中输入的内容
TextBox newTitle = (TextBox)e.Item.FindControl("TextBox1");
//定义SQL语句
string sqlCom = "update us set nam='" + newTitle.Text + "' where uid=" + empID ;
conn.ConnectionString="server=(local);user id=sa;pwd=sa;database=user";
conn.Open(); //定义命令对象
SqlCommand cmd = new SqlCommand(sqlCom,conn);
//打开数据连接
try
{
//执行SQL命令
cmd.ExecuteNonQuery();
//取消编辑
DataList1.EditItemIndex = -1;
DataList1.SelectedIndex=-1;
Sel();
}
catch(Exception err)
{
//输入异常信息
Response.Write(err.ToString());
}
finally
{
//关闭连接对象
conn.Close();
}
本文展示了一个使用ASP.NET和SQL Server进行数据更新的操作实例。通过DataList控件实现数据编辑功能,具体步骤包括获取选中项的ID、读取文本框中的新值并构建SQL更新语句,最后执行SQL命令完成数据更新。
144





