提交中的数据类型错误

时间:2014-01-26 13:10:09

标签: vb.net

我的vb.net代码存在这个大问题。一切似乎都很好,我无法弄清楚问题出在哪里。我想向Access数据库提交一条记录,但每次都会遇到此数据类型不匹配错误。我的访问数据库是字段的所有文本。除了数据类型为DATE的日期。

    Try
        'LGCodeHygNumb()
        Dim statement As String
        Dim cmd As OleDbCommand
        Dim connect As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Persist Security Info=false; Data Source=..\ClosedIn11.mdb")
        'If connect.State = ConnectionState.Closed Then
        connect.Open()
        'End If

        statement = "INSERT INTO EnrolleeDets (ID, Surname, FirstName, Othername, ReceiptNumber, Birthdate, Gender, MaritalStatus, HomeAddress, Mobile, Telephone, State, StateCode, Local_Govt, GroupType, GroupName, Location, ElectiveType, Provider, PassportDirectory, Original_RegDate, New_RenewalDate, ExpiryDate, Status, OverallStatus) values ('" & lblnumb.Text & "', '" & txtSurnanme.Text & "', '" & txtFirstname.Text & "', '" & txtNickname.Text & "', '" & txtReceiptNo.Text & "', '" & DatePicker1.Text & "', '" & cmbGender.Text & "', '" & cmbMaritalStatus.Text & "', '" & txtHomeAddr.Text & "', '" & txtMobile.Text & "', '" & TextBox8.Text & "', '" & cmbState.Text & "', '" & cmbStateCode.Text & "', '" & cmbLGACode.Text & "', '" & cmbGroupType.Text & "', '" & cmbGroupName.Text & "', '" & cmbLocation.Text & "', '" & cmbElectiveType.Text & "', '" & cmbProvider.Text & "', '" & txtPassportDir.Text & "','" & txtPassportDir.Text & "', '" & txtPassportDir.Text & "', '" & txtPassportDir.Text & "', '" & txtPassportDir.Text & "', '" & txtPassportDir.Text & "')"
        'MsgBox(statement)
        cmd = New OleDbCommand(statement, connect)
        cmd.ExecuteNonQuery()
        'MsgBox(lblnumb.Text & " added under " & txtSurnanme.Text & " !", vbInformation)
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
    'connect.Close()

2 个答案:

答案 0 :(得分:1)

在你的陈述中:

statement = "INSERT INTO EnrolleeDets (ID, Surname, FirstName, Othername, ReceiptNumber, Birthdate, Gender, MaritalStatus, HomeAddress, Mobile, Telephone, State, StateCode, Local_Govt, GroupType, GroupName, Location, ElectiveType, Provider, PassportDirectory, Original_RegDate, New_RenewalDate, ExpiryDate, Status, OverallStatus) values ('" & lblnumb.Text & "', '" & txtSurnanme.Text & "', '" & txtFirstname.Text & "', '" & txtNickname.Text & "', '" & txtReceiptNo.Text & "', '" & DatePicker1.Text & "', '" & cmbGender.Text & "', '" & cmbMaritalStatus.Text & "', '" & txtHomeAddr.Text & "', '" & txtMobile.Text & "', '" & TextBox8.Text & "', '" & cmbState.Text & "', '" & cmbStateCode.Text & "', '" & cmbLGACode.Text & "', '" & cmbGroupType.Text & "', '" & cmbGroupName.Text & "', '" & cmbLocation.Text & "', '" & cmbElectiveType.Text & "', '" & cmbProvider.Text & "', '" & txtPassportDir.Text & "','" & txtPassportDir.Text & "', '" & txtPassportDir.Text & "', '" & txtPassportDir.Text & "', '" & txtPassportDir.Text & "', '" & txtPassportDir.Text & "')"

您的专栏Original_RegDate, New_RenewalDate, ExpiryDate'" & txtPassportDir.Text & "', '" & txtPassportDir.Text & "', '" & txtPassportDir.Text & "'

匹配

我猜想txtPassportDir.Text不包含日期。

答案 1 :(得分:0)

您应该使用OleDbParameters创建命令。

statement = "INSERT INTO EnrolleeDets (Surname, Birthdate, Status) values (@ID, @Birthdate, @Status);"

确保添加的值具有正确的数据类型:

cmd.Parameters.AddWithValue("@Surname", Me.TextBox1.Text) ' VarChar
cmd.Parameters.AddWithValue("@Birthdate", Date.Parse(Me.TextBox2.Text)) 'DateTime
cmd.Parameters.AddWithValue("@Status", Integer.Parse(Me.TextBox3.Text)) 'Int