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我剛剛寫了相同的答案,打了我40秒:) –
Faill! o.O這就是我得到的複製粘貼 –
更新過程仍然不起作用,雖然xS –