【问题标题】:Update statement doesn't update my data更新语句不会更新我的数据
【发布时间】:2014-01-13 11:09:53
【问题描述】:

我想在 access 数据库的 Employee 表中更新/编辑我的用户数据。

当我完成我想要更改的字段(姓名、姓氏等)时,它会给我 数据更新 但是当我刷新表格时,数据没有改变 - 一直更新了。

例如我想要执行的更改 - 将名称从 Luke 更改为 Taylor,等等。

我哪里出错了?代码中的错误在哪里?我用于将用户添加到数据库的代码是否以某种方式影响了我的更新代码?

我添加用户的代码与更新的代码几乎相同,除了查询,它工作正常。

private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            command.Connection = myConnection;
            command.CommandText = "Update Employee set Name = @Name, LastName = @LastName, UserName = @UserName, Password = @Password, E_mail = @E_mail, Address = @Address WHERE ID = @ID";
            command.Parameters.AddWithValue("@ID", userID.Text);
            command.Parameters.AddWithValue("@Name", name.Text);
            command.Parameters.AddWithValue("@LastName", lastName.Text);
            command.Parameters.AddWithValue("@UserName", userName.Text);
            command.Parameters.AddWithValue("@Password", pass.Text);
            command.Parameters.AddWithValue("@E_mail", email.Text);
            command.Parameters.AddWithValue("@Address", address.Text);

            myConnection.Open();
            command.ExecuteNonQuery();
            MessageBox.Show("User updated!");
            myConnection.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

添加用户数据的代码

private void button1_Click(object sender, EventArgs e)
    {
        try
        {

            command.Connection = myConnection;
            command.CommandText = "Insert into Employee (ID, Name, LastName, UserName, Password, E_mail, Address)" + "values (@ID, @Name, @LastName, @UserName, @Password, @E_mail, @Address)";
            command.Parameters.AddWithValue("@ID", userID.Text);
            command.Parameters.AddWithValue("@Name", name.Text);
            command.Parameters.AddWithValue("@LastName", lastName.Text);
            command.Parameters.AddWithValue("@UserName", userName.Text);
            command.Parameters.AddWithValue("@Password", pass.Text);
            command.Parameters.AddWithValue("@E_mail", email.Text);
            command.Parameters.AddWithValue("@Address", address.Text);

            myConnection.Open();
            command.ExecuteNonQuery();
            MessageBox.Show("User added!");
            myConnection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

感谢您的回复和帮助


我仍然没有解决方案。我已经尝试了很多东西,但我只是没有得到正确的答案。

我当前的代码

 try
        {
            OleDbConnection myConnection = new OleDbConnection("\\DATABASE PATH");
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = myConnection;
            cmd.CommandText = "UPDATE Employees SET Name = @Name, LastName = @LastName, UserName = @UserName, Password = @Password, E_mail = @E_mail, Address = @Address WHERE ID = @";

            cmd.Parameters.AddWithValue("@ID", userID.Text);
            cmd.Parameters.AddWithValue("@Name", name.Text);
            cmd.Parameters.AddWithValue("@LastName", lastName.Text);
            cmd.Parameters.AddWithValue("@UserName", userName.Text);
            cmd.Parameters.AddWithValue("@Password", pass.Text);
            cmd.Parameters.AddWithValue("@E_mail", eMail.Text);
            cmd.Parameters.AddWithValue("@Address", address.Text);

            myConnection.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("User successfully added.");
            myConnection.Close();
        }
       catch (Exception ex)
       {
           MessageBox.Show(ex.Message);
       }

【问题讨论】:

  • 你能发布你用来添加用户的代码吗?可能有助于发现问题。
  • 确定,我刚刚编辑了我的帖子
  • 您是否正在创建自己的 ID?您是手动生成它们吗?还是数据库应该自动生成它们?看起来好像您正在创建自己的 ID。您是否在强制执行唯一性?
  • 我正在手动创建它们。我有一个输入 ID 的文本框字段。我真的不知道如何生成它们。是的,用户应该有唯一的 ID
  • 最可能的解释似乎是当点击更新按钮时,ID 发生了变化。只需仔细检查更新代码中最终的 ID 是否与数据库中成功添加的行的 ID 匹配。也许下面的答案是正确的,但我不太明白他的表达方式。

标签: c# .net database oledbconnection oledbcommand


【解决方案1】:

因为你的身份证在哪里。

您还通过以下方式更改/更新您的 ID:

command.Parameters.AddWithValue("@ID", userID.Text);

编译器在数据库中找不到这个新 ID,因为您在查询中保留了 where ID=@ID 条件。

当您只更新名称和其他字段时,查询变为:

Update Employee set Name = 'Name', LastName = 'LastName', UserName = 'UserName', Password = 'Password', E_mail = 'E_mail', Address = 'Address' WHERE ID = ''";

在这种情况下,您的 ID 可能会保持空白。

【讨论】:

  • 这意味着我应该删除 command.Parameters.AddWithValue("@ID", userID.text);但保持 **WHERE 条件 - 就像你上面写的那样?
  • 不完全一样,但你需要保持你的 userID.text 永久或数据库中存在的值
  • 我如何做到这一点?
【解决方案2】:

在您的更新代码中尝试以下操作:

command.CommandText = "UPDATE Employee SET [Name] = ?, LastName = ?, UserName = ?, [Password] = ?, [E_mail] = ?, Address = ? WHERE [ID] = ?";

command.Parameters.AddWithValue("@Name", name.Text);
command.Parameters.AddWithValue("@LastName", lastName.Text);
command.Parameters.AddWithValue("@UserName", userName.Text);
command.Parameters.AddWithValue("@Password", pass.Text);
command.Parameters.AddWithValue("@E_mail", email.Text);
command.Parameters.AddWithValue("@Address", address.Text);
command.Parameters.AddWithValue("@ID", userID.Text);

参数必须按照它们在CommandText 中出现的顺序。此答案由:Microsoft Access UPDATE command using C# OleDbConnection and Command NOT working

提出

这里概述了这样做的原因:http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters(v=vs.110).aspx

OLE DB .NET 提供程序不支持传递命名参数 SQL 语句或存储过程的参数 当 CommandType 设置为 Text 时的 OleDbCommand。在这种情况下, 必须使用问号 (?) 占位符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    相关资源
    最近更新 更多