我为DataGrid写了一个更新的方法 private void UpdateRecord()
{
SqlConnection conn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=123");
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("SELECT * FROM Employees WHERE EmployeeID=1", conn);
SqlCommandBuilder cmdBld = new SqlCommandBuilder(sda);
conn.Open();
sda.Fill(dt); dt.Rows[0]["LastName"] = "aaa";
dt.Rows[0]["firstName"] = "bbb";
dt.Rows[0]["City"] = "ccc";
dt.Rows[0]["HomePhone"] = "84848454"; sda.Update(dt.Select(null,null,DataViewRowState.ModifiedCurrent)); conn.Close();
}
/////////////////////////////////////////////////////
我上面的sda.SelectCommand = new SqlCommand("SELECT * FROM Employees WHERE EmployeeID=1", conn)这里的EmployeeID=.. 要是它的主键值,才可以更新当前行,可我不知道这么该怎么写?
{
SqlConnection conn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=123");
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("SELECT * FROM Employees WHERE EmployeeID=1", conn);
SqlCommandBuilder cmdBld = new SqlCommandBuilder(sda);
conn.Open();
sda.Fill(dt); dt.Rows[0]["LastName"] = "aaa";
dt.Rows[0]["firstName"] = "bbb";
dt.Rows[0]["City"] = "ccc";
dt.Rows[0]["HomePhone"] = "84848454"; sda.Update(dt.Select(null,null,DataViewRowState.ModifiedCurrent)); conn.Close();
}
/////////////////////////////////////////////////////
我上面的sda.SelectCommand = new SqlCommand("SELECT * FROM Employees WHERE EmployeeID=1", conn)这里的EmployeeID=.. 要是它的主键值,才可以更新当前行,可我不知道这么该怎么写?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货