鄙人不才,遇一问题,愿大家为之解忧!
protected void Page_Load(object sender, EventArgs e)
{
string sConnectionString;
sConnectionString = "Data Source=LQG-PC;Initial Catalog = test;User ID=sa;PassWord=73333xxm;";
SqlConnection Conn = new SqlConnection(sConnectionString);
Conn.Open();
string commandString= "Select BianH AS 编号,ZhuCM AS 注册名,XingM AS 姓名,XingB AS 性别 FROM menber";
SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString, Conn);
SqlCommandBuilder cbd = new SqlCommandBuilder(dataAdapter);
DataSet ds = new DataSet();
dataAdapter.Fill(ds, "menber");
DataTable dataTable = ds.Tables["menber"];
string filterStr = "性别='男'";
string strxingb = null;
DataRow[] XingB = dataTable.Select(filterStr);
for (int i = 0; i < XingB.Length; i++)
{
DataRow temp = XingB[i];
strxingb += temp["性别"] = "men";
XingB[i] = temp;
}
dataAdapter.Update(ds, "menber");
ds.Tables["menber"].AcceptChanges();
GridView1.DataSource = dataTable;
GridView1.DataBind();
}
以上为完整代码。解析:
数据库有一数据表“menber”,并填充到数据集中,现在更新数据集ds的数据表,把性别字段为“男”的改为“men”。我的目的是同时把这个修改更新到真正的数据表中。现在问题是按上面代码是可以做到,但是如果把黄色背景的两句交换位置,那么真正的数据表就更新不了。为什么?谢谢!
MM们