所以我正在尝试使用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
答案 0 :(得分:1)
您的insert命令不是有效的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添加为最后一个参数。