2012-11-19 37 views
1

所以我试图使用DataAdapter和Datasets将联系人添加到我的数据库。但是当我尝试添加数据时,我仍然收到相同的错误(请参阅标题)C#OleDbException未处理:没有给出一个或多个必需参数的值

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

添加用户代码

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

回答

1

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

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

更新commad文本丢失一个逗号

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作为最后一个参数。

+0

+1我刚刚写了相同的答案,打了我40秒:) –

+0

Faill! o.O这就是我得到的复制粘贴 –

+0

更新过程仍然不起作用,虽然xS –

相关问题