2014-01-13 54 views
1

我想在访问数据库的Employee表中更新/编辑我的用户数据。更新声明不会更新我的数据

当我完成,我想改变(名字,姓氏等)领域,它给了我更新数据,但是当我刷新表,数据并没有改变 - 被更新。

变化我想执行例如 - 卢克改名泰勒等

我在哪里出了错?代码中的错误在哪里,我的代码将用户添加到数据库不知何故影响我的更新代码?

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

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); 
     } 
+0

你可以发布你用来添加用户的代码吗?可能有助于确定问题。 –

+0

当然,我只是编辑了我的文章 – fkr

+0

您是否正在创建自己的ID?你是否手动生成它们?或者DB应该自动生成它们?它看起来好像你正在创建你自己的ID。你是否执行唯一性? –

回答

1

尝试在你的更新代码如下:

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设置为文本时OleDbCommand。在这种情况下,必须使用 问号(?)占位符。

2

它,因为你在条件ID。

你也在改变/通过更新您的ID:

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

这个新的ID没有被编译在数据库中发现,因为你一直在您的查询,其中ID = @ ID条件。

当你刚刚更新的名字和其他领域再查询就会变成:

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

你的ID可能在这种情况下保持空白。

+0

这意味着我应该删除** command.Parameters.AddWithValue(“@ ID”,userID.text);但保持** WHERE **条件 - 就像上面写的那个一样? – fkr

+0

不完全如此,但你需要保持你的userID.text permanant或数据库中存在的值 –

+0

,我该如何实现? – fkr