我试图将用户输入的数据插入Windows窗体到Access数据库。在表格中,我使用了一个文本框,在db中由查找表填充的组合框,以及2个dateTimePickers。通过我自己的调试尝试,我想我已经缩小到dateTimePickers,但我不是100%确定。我没有收到任何错误,没有任何异常被捕获,并且在执行查询后显示“成功消息”,但没有任何内容添加到数据库中。插入Access数据库使用Windows窗体不工作,但不会引发错误
这是从形式的方法调用:
try
{
//get values from form
int updatedRow;
int ethnicityID = Convert.ToInt32(comboBoxEthnicity.SelectedValue);
int genderID = Convert.ToInt32(comboBoxGender.SelectedValue);
int raceID = Convert.ToInt32(comboBoxRace.SelectedValue);
DateTime dob = dateTimePickerDoB.Value;
DateTime dateOfConsent = dateTimePickerDateOfConsent.Value;
int hhpid = Convert.ToInt32(textBoxHHPID.Text);
//add new patient to db
updatedRow = TrialDB.writePatient(dataSetTrial, ethnicityID, genderID, raceID, dob, dateOfConsent, hhpid);
//success message, number of new patients added
MessageBox.Show(updatedRow.ToString() + " patient record has been added to the database.");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
,这里是从中间层的方法:
public static int writePatient(DataSetTrial dataSetTrial, int ethnicityID, int genderID, int raceID, DateTime dob, DateTime dateOfConsent, int hhpid)
{
int addedRow;
OleDbDataAdapter writePatientAdapter;
try
{
//configure adapter
writePatientAdapter = new OleDbDataAdapter();
//insert command
writePatientAdapter.InsertCommand = new OleDbCommand();
writePatientAdapter.InsertCommand.CommandText = "INSERT INTO Patient (EthnicityID,GenderID,RaceID,DateOfBirth, " +
" DateOfConsent,HomeHealthPatientID) " +
" VALUES (?,?,?,?,?,?)";
writePatientAdapter.InsertCommand.Connection = new OleDbConnection(connectString);
//insert command paramaters
writePatientAdapter.InsertCommand.Parameters.Add("@EthnicityID", OleDbType.Integer);
writePatientAdapter.InsertCommand.Parameters["@EthnicityID"].Value = ethnicityID;
writePatientAdapter.InsertCommand.Parameters.Add("@GenderID", OleDbType.Integer);
writePatientAdapter.InsertCommand.Parameters["@GenderID"].Value = genderID;
writePatientAdapter.InsertCommand.Parameters.Add("@RaceID", OleDbType.Integer);
writePatientAdapter.InsertCommand.Parameters["@RaceID"].Value = raceID;
writePatientAdapter.InsertCommand.Parameters.Add("@DateOfBirth", OleDbType.Date);
writePatientAdapter.InsertCommand.Parameters["@DateOfBirth"].Value = dob;
writePatientAdapter.InsertCommand.Parameters.Add("@DateOfConsent", OleDbType.Date);
writePatientAdapter.InsertCommand.Parameters["@DateOfConsent"].Value = dateOfConsent;
writePatientAdapter.InsertCommand.Parameters.Add("@HomeHealthPatientID", OleDbType.Integer);
writePatientAdapter.InsertCommand.Parameters["@HomeHealthPatientID"].Value = hhpid;
writePatientAdapter.InsertCommand.Connection.Open();
addedRow = writePatientAdapter.Update(dataSetTrial, "Patient");
}
catch (Exception)
{
throw new ApplicationException(dbOrDeErrorMsg);
}
writePatientAdapter.InsertCommand.Connection.Close();
return addedRow;
}
当我的DateTime变量添加到我的收藏在调试模式它无法评估领先的{来自dateTimePicker的格式。这就是为什么我认为这是DateTime引起我的麻烦。我直接在数据库中尝试了INSERT语句并且它可以工作,但是当我手动输入日期格式时,我无法解释日期格式。网络显示了很多这样做的麻烦,但我找到的修复都没有帮助。任何帮助,将不胜感激。
谢谢!这工作!我对开发还很陌生。这是我在教室之外写的第一个Windows Form应用程序。我认为我刚刚在学校学到的东西上穿过了电线。上述代码中唯一改变的是将var InsertCommand声明移出try块,以便我可以在finally块中关闭它。非常感谢您的帮助。经过两天的研究并将我的头撞在键盘上,我感激不尽。 – rustan13 2015-02-24 20:58:59