使用Windows窗体插入Access DB不起作用,但未引发错误



我正试图将用户输入的数据插入Access DB中的Windows窗体中。在表单中,我使用了一个文本框、由数据库中的查找表填充的组合框和2个dateTimePickers。通过我自己的调试尝试,我想我已经将其缩小到了dateTimePickers,但我不能100%确定。我没有收到任何错误,没有发现异常,在我执行查询后会显示"成功消息",但没有向数据库中添加任何内容。

这是来自以下表单的方法调用:

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语句,它很有效,但当我手动输入时,我无法解释日期格式。web显示这样做有很多麻烦,但我找到的修复程序都没有帮助。如果有任何帮助,我们将不胜感激。

您混合了两种不同的数据库访问方式。一方面,您正在使用一个适配器,该适配器用于处理数据集。您可以对数据集中的表进行更改,然后在适配器上调用update。但是,您自己通过传递参数值来控制适配器内的命令对象,只有在没有适配器的情况下从一开始就使用命令时,您才会这样做。

我建议你搜索一下命令的正确使用方法

您离解决方案不远了,只需实例化insert命令,您已经在做了,但只需为它创建自己的变量,不使用适配器,并通过insert命令自己进行更新。

您需要在insertcommand上使用以下api:

https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.executenonquery(v=vs.110).aspx

只要丢失适配器,在本例中就不需要它了。

现在,您没有看到任何错误的原因是因为也没有发生任何更新。您没有对数据集进行任何更改,对适配器进行这样的更新也不会有任何作用。

public static int writePatient(DataSetTrial dataSetTrial, int ethnicityID, int genderID, int raceID, DateTime dob, DateTime dateOfConsent, int hhpid)
{
    int addedRow;
    //OleDbDataAdapter writePatientAdapter; don't need this
    try
    {
    //configure adapter
    //writePatientAdapter = new OleDbDataAdapter(); no, you don't need it
    //insert command
    var InsertCommand = new OleDbCommand();
    InsertCommand.CommandText = "INSERT INTO Patient (EthnicityID,GenderID,RaceID,DateOfBirth, " +
                                                                " DateOfConsent,HomeHealthPatientID) " +
                                                                " VALUES (?,?,?,?,?,?)";
    InsertCommand.Connection = new     OleDbConnection(connectString);
     //insert command paramaters
    InsertCommand.Parameters.Add("@EthnicityID", OleDbType.Integer);
     writePatientAdapter.InsertCommand.Parameters["@EthnicityID"].Value = ethnicityID; 

    InsertCommand.Parameters.Add("@GenderID", OleDbType.Integer);
     writePatientAdapter.InsertCommand.Parameters["@GenderID"].Value = genderID; 
    InsertCommand.Parameters.Add("@RaceID", OleDbType.Integer);
     writePatientAdapter.InsertCommand.Parameters["@RaceID"].Value = raceID;
    InsertCommand.Parameters.Add("@DateOfBirth", OleDbType.Date);
     writePatientAdapter.InsertCommand.Parameters["@DateOfBirth"].Value = dob; 
    InsertCommand.Parameters.Add("@DateOfConsent", OleDbType.Date);
     writePatientAdapter.InsertCommand.Parameters["@DateOfConsent"].Value = dateOfConsent; 
    InsertCommand.Parameters.Add("@HomeHealthPatientID", OleDbType.Integer);
    InsertCommand.Parameters["@HomeHealthPatientID"].Value = hhpid; 
    InsertCommand.Connection.Open();
    addedRow = InsertCommand.executeNonQuery();
     }
     catch (Exception)
     {
         throw new ApplicationException(dbOrDeErrorMsg);
     }
         InsertCommand.Connection.Close();
         return addedRow;
}

这是我认为应该让你开始的一点,没有测试它,你现在可能会遇到不同的问题。

最新更新