2015-02-24 95 views
0

我试图将用户输入的数据插入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语句并且它可以工作,但是当我手动输入日期格式时,我无法解释日期格式。网络显示了很多这样做的麻烦,但我找到的修复都没有帮助。任何帮助,将不胜感激。

回答

1

您正在混合2种不同的数据库访问方式。 一方面,您正在使用适用于与数据集一起工作的适配器。您可以对数据集中的表进行更改,然后在适配器上调用更新。 但是,您正在控制适配器内部的commmand对象自己传递参数值,只有在不使用适配器的情况下从头开始使用这些命令时,您才会这样做。

我建议你谷歌了一下周围,找到正确使用命令

你是不远处一个soluton只是实例化插入命令,你是这样做了,但只是创建您自己的变量为它,不要使用适配器,并自己通过insert命令进行更新。

需要使用此API上的InsertCommand:

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; 
} 

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

+0

谢谢!这工作!我对开发还很陌生。这是我在教室之外写的第一个Windows Form应用程序。我认为我刚刚在学校学到的东西上穿过了电线。上述代码中唯一改变的是将var InsertCommand声明移出try块,以便我可以在finally块中关闭它。非常感谢您的帮助。经过两天的研究并将我的头撞在键盘上,我感激不尽。 – rustan13 2015-02-24 20:58:59

相关问题