在vb.net中的数据库中添加记录

时间:2018-07-08 15:34:09

标签: database vb.net

我是VB的新手,我必须在程序中添加数据库,但是当以表单的形式向数据库中添加函数时,例如添加新记录,就会出现错误,而且我不知道如何解决它。

  

参数@TxtGender没有默认值

这是图片: Error @TxtGender has no default value

这是添加新记录的代码

 Private Sub BtnAddRec_Click(sender As Object, e As EventArgs) Handles BtnAddRec.Click

    Dim PregnantCheck As Boolean
    Dim MedicallyCompCheck As Boolean

    If ChkPregnantRec.Checked Then

        PregnantCheck = True
    Else

        PregnantCheck = False

    End If

    If ChkPregnantRec.Checked Then

        MedicallyCompCheck = True
    Else

        MedicallyCompCheck = False

    End If

    Call connection()

    Dim cmd = New OleDbCommand
    With cmd

        .Connection = cn
        .CommandType = CommandType.Text
        .CommandText = "INSERT INTO [patient details] ([First name],[Surname],[Gender],[Date of birth],[Place of birth],[Phone number],[Street Address],[Suburb],[Health Insurance],[Post code],[Pregnant],[Medically compromised]) VALUES (@TxtFirstnameRec,@TxtSurnameRec,@TxtGenderRec,@TxtDobRec,@TxtPlaceofbirthRec,@TxtPhoneRec,@TxtStreetRec,@TxtSuburb,@TxtPostcodeRec,@TxtHealthinsuranceRec,@ChkPregnantRec,@ChkMedicallycompromisedRec)"

        .Parameters.Add(New OleDbParameter("@TxtFirstnameRec", OleDbType.VarChar, 255, TxtFirstnameRec.Text))
        .Parameters.Add(New OleDbParameter("@TxtSurnameRec", OleDbType.VarChar, 255, TxtSurnameRec.Text))
        .Parameters.Add(New OleDbParameter("@TxtDobRec", OleDbType.Date, 255, dtpDOB.Value.Date))
        .Parameters.Add(New OleDbParameter("@TxtGenderRec", OleDbType.VarChar, 255, TxtGenderRec.Text))
        .Parameters.Add(New OleDbParameter("@TxtPlaceofbirthRec", OleDbType.VarChar, 255, TxtPlaceofbirthRec.Text))
        .Parameters.Add(New OleDbParameter("@TxtStreetRec", OleDbType.VarChar, 255, TxtStreetRec.Text))
        .Parameters.Add(New OleDbParameter("@TxtSuburb", OleDbType.VarChar, 255, TxtSuburbRec.Text))
        .Parameters.Add(New OleDbParameter("@TxtPostcodeRec", OleDbType.VarChar, 255, TxtPostcodeRec.Text))
        .Parameters.Add(New OleDbParameter("@TxtPhoneRec", OleDbType.VarChar, 255, TxtPhoneRec.Text))
        .Parameters.Add(New OleDbParameter("@TxtHealthinsuranceRec", OleDbType.VarChar, 255, TxtHealthinsuranceRec.Text))
        .Parameters.Add(New OleDbParameter("@ChkPregnantRec", OleDbType.Boolean, 255, ChkPregnantRec.Checked))
        .Parameters.Add(New OleDbParameter("@ChkMedicallycompromisedRec", OleDbType.Boolean, 255, ChkMedicallycompromisedRec.Checked))

        .Parameters("@TxtFirstnameRec").Value = TxtFirstnameRec.Text
        .Parameters("@TxtSurnameRec").Value = TxtSurnameRec.Text

        '.Parameters("@TxtDobRec").Value = dtpDOB.Value.ToStr
        .Parameters("@TxtGenderRec").Value = TxtGenderRec.Text
        .Parameters("@TxtPlaceofbirthRec").Value = TxtPlaceofbirthRec.Text
        .Parameters("@TxtStreetRec").Value = TxtStreetRec.Text
        .Parameters("@TxtSuburb").Value = TxtSuburbRec.Text
        .Parameters("@TxtPostcodeRec").Value = TxtPhoneRec.Text
        .Parameters("@TxtPhoneRec").Value = TxtFirstnameRec.Text
        .Parameters("@TxtHealthinsuranceRec").Value = TxtHealthinsuranceRec.Text

        .Parameters("@ChkPregnantRec").Value = ChkPregnantRec.Checked
        .Parameters("@ChkMedicallycompromisedRec").Value = ChkMedicallycompromisedRec.Checked

很抱歉,我放置了大量代码,但是我不知道该怎么办。数据库位于程序的bin中。

The data types and values in the database

如果有人可以帮助我,将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

在OleDb中,参数是按位置排列的,即忽略其名称,但必须按照与SQL文本中出现的顺序相同的顺序添加它们。

由于@TxtGenderRec在SQL文本中位于第三位置,因此必须将其添加在@TxtSurnameRec参数之后,以代替@TxtDobRec参数。

其他参数也不匹配。