我知道这个话题已经讨论了很多次,但我仍然没有解决我的问题..
好的,我有一个表格可以将注册数据插入 MS Access 数据库(2007),但是我的代码没有将数据插入数据库,并且没有错误,这里是代码:
OleDbConnection cn = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = Data/db_klinik.mdb");
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter adapter = new OleDbDataAdapter();
private void btnSave_Click(object sender, EventArgs e)
{
string idCard = this.txtID.Text;
string name = this.txtName.Text;
DateTime dateBirth = this.dateEdit1.DateTime;
cn.Open();
cmd.CommandText = "Insert into tb_reg (id, name, dateBirth, blood_type) Values(@id,@name,@dateBirth)";
cmd.Parameters.AddWithValue("@id", idCard);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@dateBirth", dateBirth.ToString());
adapter.InsertCommand = cmd;
int result = cmd.ExecuteNonQuery();
if (result > 0)
MessageBox.Show("Succesfully added");
else
MessageBox.Show("try again");
cn.Close();
}
消息框总是显示“成功添加”。