INSERT into Access DB using Windows Forms 不工作但不抛出错误

INSERT into Access DB using Windows Forms not working but not throwing an error

我正在尝试将用户输入的数据插入 Windows 表单到 Access 数据库中。在表单中,我使用了一个文本框、由数据库中的查找表填充的组合框和 2 个 dateTimePickers。通过我自己的调试尝试,我想我已经将范围缩小到 dateTimePickers,但我不是 100% 确定。我没有收到任何错误,没有捕获到异常,并且在我执行查询后显示了 "Success Message",但没有向数据库中添加任何内容。

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

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 语句并且它起作用了,但是当我手动输入它时我无法解释日期格式。网上显示这样做有很多麻烦,但 none 我发现的修复有帮助。任何帮助将不胜感激。

您混合了两种不同的数据库访问方式。 一方面,您正在使用旨在处理数据集的适配器。您将对数据集中的 table 进行更改,然后在适配器上调用更新。 但是您正在控制适配器内部传递参数值的命令对象,只有在没有适配器的情况下从头开始使用命令时才会这样做。

我建议你 google 稍微了解一下命令的正确用法

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

您需要在插入命令中 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;
}

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