我的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()
此鏈接會解決你目前(和將來的)問題:http://msdn.microsoft.com/en-us/ library/system.data.oledb.oledbparameter(v = vs.110).aspx –
請注意使用內聯SQL進行SQL注入。 http://en.wikipedia.org/wiki/SQL_injection –
謝謝羅傑。我想我忘了補充一點,我是vb.net編程的新手。一遍又一遍仔細檢查代碼,我似乎沒有發現任何錯誤的構圖。除了每次我嘗試提交數據庫時,我總是收到數據類型不匹配。 – Joseph