【问题标题】:C# OleDbException was unhandled: No value given for one or more required parametersC# OleDbException 未处理:没有为一个或多个必需参数提供值
【发布时间】:2012-11-07 14:17:21
【问题描述】:

所以我正在尝试使用 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

【问题讨论】:

    标签: c# dataset sql-update dataadapter sql-insert


    【解决方案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 作为最后一个参数。

    【讨论】:

    • 失败! o.O 这就是我复制粘贴所得到的
    • 你在使用 OleDB 吗?一些提供者要求参数在查询中出现的确切顺序。将 @contactID 作为 AddWithValue 序列中的最后一个参数移动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多