如何只更新datagrid中的一行

时间:2012-05-28 06:02:07

标签: c#-4.0 ms-access-2007

当我运行我的代码然后更新成功但我只想更新一行时,我遇到了问题。通过我的代码时,所有数据都会更新。

我的代码是:

代码

protected void imgbtn_Save_Click(object sender, EventArgs e)
{

        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = "update Companies set CompanyFName='" + txt_ComName.Text + "',CompanySName='" + txt_ShortName.Text + "',CompanyeMail='" + txt_email.Text + "',CompanyWebsite='" + txt_website.Text + "'where CompanyId=CompanyId";

        cmd.Connection = conn;
        OleDbDataAdapter da = new OleDbDataAdapter();
        da.UpdateCommand = cmd;

        cmd.ExecuteNonQuery();
        conn.Close();
        BindGridData();
        lblError.Font.Bold = true;
        lblError.Font.Size = 11;
        lblError.Text = "You have successfully modified the case!";
    }

我不知道为什么?

Plz建议我。

“谢谢”

1 个答案:

答案 0 :(得分:0)

首先使用OleDbParameter。看看这个帖子。 how to update a table using oledb parameters?

您的代码的问题在于您使用BindGridData();刷新完整数据源可能存在其他记录在代码中的其他位置更新的情况,并且您的BindGridData()似乎正在获取数据再次从数据库。您可以将现有数据源存储在临时位置,然后将其更新并将其绑定到数据网格,而不是通过db获取最新信息

相关问题