2014-01-26 34 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