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