1

所以我正在尝试使用 DataAdapter 和 Datasets 将联系人添加到我的数据库中。但是当我尝试添加数据时,我总是遇到同样的错误(见标题)

此外,当我更新我的数据时,它不会给出任何错误,但它也不会更新任何东西。

添加用户代码

public void AddUser(Contact contact) {
  command.Connection = getConnection();
  command.CommandText = "INSERT INTO tblContact VALUES(Id = @contactid, LastName = @lastname, FirstName = @firstname, Address = @address"
    + "PostCode = @postcode, City = @city, Gender = @gender, Blocked = @blocked)";

  command.Parameters.AddWithValue("@contactid", contact.AccountId);
  command.Parameters.AddWithValue("@lastname", contact.LastName);
  command.Parameters.AddWithValue("@firstname", contact.FirstName);
  command.Parameters.AddWithValue("@address", contact.Address);
  command.Parameters.AddWithValue("@postcode", contact.PostCode);
  command.Parameters.AddWithValue("@city", contact.City);
  bool gender = (contact.Gender == Gender.Male ? true : false);
  command.Parameters.AddWithValue("@gender", gender);
  command.Parameters.AddWithValue("@blocked", contact.Blocked);
  try {
    adapter.InsertCommand = command;
    adapter.Update(dsCon, "tblContact");
    adapter.Fill(dsCon, "tblContact");
  }
  catch (Exception e) {
    String code = e.Message;
  }

}

用于更新用户的代码

 public void ModifyUser(Contact contact) 
 {
  command.Connection = getConnection();
  command.CommandText = "UPDATE tblContact SET LastName = @lastname, FirstName = @firstname, Address = @address" 
    + "PostCode = @postcode, City = @city, Gender = @gender, Blocked = @blocked " 
    + "WHERE Id = @contactid";

  command.Parameters.AddWithValue("@contactid", contact.AccountId);
  command.Parameters.AddWithValue("@lastname", contact.LastName);
  command.Parameters.AddWithValue("@firstname", contact.FirstName);
  command.Parameters.AddWithValue("@address", contact.Address);
  command.Parameters.AddWithValue("@postcode", contact.PostCode);
  command.Parameters.AddWithValue("@city", contact.City);
  command.Parameters.AddWithValue("@gender", contact.Gender);
  command.Parameters.AddWithValue("@blocked", contact.Blocked);

  adapter.UpdateCommand = command;
  adapter.Update(dsCon, "tblContact");
  adapter.Fill(dsCon, "tblContact");
}

我的表单中启动这些过程的代码

private void btnSave_Click(object sender, EventArgs e) {
  Contact currentContact = new Contact();
  currentContact.AccountId = Int32.Parse(lblID.Text);
  currentContact.LastName = txtLastName.Text;
  currentContact.FirstName = txtName.Text;
  currentContact.Address = txtStreet.Text;
  currentContact.PostCode = Int32.Parse(txtPostalCode.Text);
  currentContact.City = txtCity.Text;
  currentContact.Gender = (rdbMale.Checked == true ? Gender.Male : Gender.Female);
  currentContact.Blocked = chkBlocked.Checked;
  currentContact.Address = txtStreet.Text;
  if(isNewContact){
    currentContact.AccountId = (manager.GetContacts().Last().AccountId + 1);
    manager.GetOleDbManager().AddUser(currentContact);
  } else {
        manager.GetOleDbManager().ModifyUser(currentContact);
  }
  currentContact.Categories = new List<Category>();
  foreach (Object c in lstCategories.SelectedItems) {
    currentContact.Categories.Add((Category)c);
  }

  isNewContact = false;

}

如果您能提供帮助,那就太好了,这是我正在使用的数据库的屏幕截图 http://i50.tinypic.com/2s93klk.png

4

1 回答 1

1

您的插入命令不是有效的 sql INSERT 语句。

command.CommandText = "INSERT INTO tblContact VALUES(@contactid, @lastname, @firstname,"+ 
                      "@address,@postcode, @city, @gender, @blocked)";

更新逗号文本缺少逗号

command.CommandText = "UPDATE tblContact SET LastName = @lastname, FirstName = @firstname," +  
                      "Address = @address, PostCode = @postcode, City = @city, " + 
                      "Gender = @gender, Blocked = @blocked WHERE Id = @contactid";

但是,该命令也会失败,因为某些 OleDb 提供程序(如 Microsoft.ACE.OleDb.xx)要求 ParameterCollection 具有参数在 sql 文本中出现的确切顺序。(不支持命名参数)。您的更新语句包含 @contactid 作为最后一个参数,而您将其添加为第一个参数。您可以尝试更改 AddWithValue 序列,添加 @contactid 作为最后一个参数。

于 2012-11-19T16:06:37.083 回答