2014-01-26 32 views
0

我的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() 
+0

此链接会解决你目前(和将来的)问题:http://msdn.microsoft.com/en-us/ library/system.data.oledb.oledbparameter(v = vs.110).aspx –

+0

请注意使用内联SQL进行SQL注入。 http://en.wikipedia.org/wiki/SQL_injection –

+0

谢谢罗杰。我想我忘了补充一点,我是vb.net编程的新手。一遍又一遍仔细检查代码,我似乎没有发现任何错误的构图。除了每次我尝试提交数据库时,我总是收到数据类型不匹配。 – Joseph

回答

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不包含日期。

+0

谢谢Aaron!那样做了!问题解决了!!!谢谢。 – Joseph

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