有关C#与SQL数据编程的问题 SqlDataAdapter.update()无法更新数据库
dataset中的数据已经更改了,但是数据库中的没有更新。privatevoidbutton4_Click(objectsender,EventArgse){string...
dataset中的数据已经更改了,但是数据库中的没有更新。
private void button4_Click(object sender, EventArgs e)
{
string a = "";
string b = "";
SqlDataAdapter sda2 = new SqlDataAdapter("select * from D_ZD", StrConnection);
DataSet ds2 = new DataSet();
sda2.Fill(ds2);
dataGridView2.DataSource = ds2.Tables[0];
sda2.UpdateCommand = new SqlCommand("update D_ZD set LandNo = " + a + " where ZDId = " + b, objConnection);
sda2.UpdateCommand.Parameters.Add("a", SqlDbType.Char, 12);
sda2.UpdateCommand.Parameters.Add("b", SqlDbType.VarChar, 32);
sda2.UpdateCommand.Parameters["a"].Value = "ZDId";
sda2.UpdateCommand.Parameters["b"].Value = "LandNo";
for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
{
a = dataGridView1["LandNo_out", i].Value.ToString();
b=dataGridView1["ZDId",i].Value.ToString();
ds2.Tables[0].Rows[i]["LandNo"] = a;
sda2.UpdateCommand = new SqlCommand("update D_ZD set LandNo = " + a + " where ZDId = " + b, objConnection);
}
sda2.Update(ds2); 展开
private void button4_Click(object sender, EventArgs e)
{
string a = "";
string b = "";
SqlDataAdapter sda2 = new SqlDataAdapter("select * from D_ZD", StrConnection);
DataSet ds2 = new DataSet();
sda2.Fill(ds2);
dataGridView2.DataSource = ds2.Tables[0];
sda2.UpdateCommand = new SqlCommand("update D_ZD set LandNo = " + a + " where ZDId = " + b, objConnection);
sda2.UpdateCommand.Parameters.Add("a", SqlDbType.Char, 12);
sda2.UpdateCommand.Parameters.Add("b", SqlDbType.VarChar, 32);
sda2.UpdateCommand.Parameters["a"].Value = "ZDId";
sda2.UpdateCommand.Parameters["b"].Value = "LandNo";
for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
{
a = dataGridView1["LandNo_out", i].Value.ToString();
b=dataGridView1["ZDId",i].Value.ToString();
ds2.Tables[0].Rows[i]["LandNo"] = a;
sda2.UpdateCommand = new SqlCommand("update D_ZD set LandNo = " + a + " where ZDId = " + b, objConnection);
}
sda2.Update(ds2); 展开
3个回答
展开全部
SqlCommandBuilder a=new SqlCommandBuilder(sda2);
sda2.Fill(ds2);
注意:确定数据库以设置了主键!
sda2.Fill(ds2);
注意:确定数据库以设置了主键!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
update这些操作用适配器不好,直接用命令写,然后执行那个命令就行了;如果一定要用适配器的话,用个sqlcommandbuild,也很方便
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询