2013-11-26 24 views
0

我寫了一些代碼插入數據的形式到ms訪問數據庫 它運行成功,並沒有提供任何錯誤 但當我在數據中看到基地我發現沒有數據插入數據庫中。 有什麼問題? 所以,夥計們,請幫助我。vb.net插入命令爲ms訪問沒有顯示任何錯誤,但沒有插入到表

這裏是我的vb.net代碼:

Private Sub btnCreateUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateUser.Click 
     Dim con2 As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\OfficeAutomationSystem.accdb; Persist Security Info=False") 
     Dim cmd As New OleDb.OleDbCommand 
     If Not con2.State = ConnectionState.Open Then 
      con2.Open() 
     End If 
     cmd.Connection = con2 
     'add data to table 
     cmd.CommandText = "INSERT INTO tblUser(emp_id, user_id, pwd, user_type) " & _ 
          " values(" & Me.cbEmpId.SelectedItem & ", '" & Me.txtUserId.Text & "','" & _ 
          Me.txtPassword.Text & "', '" & Me.cbUserType.SelectedItem & "')" 
     cmd.ExecuteNonQuery() 
     MsgBox("running") 
End Sub 

,這裏是我的MS Access數據庫的數據庫結構:

field name     dataYpe 
serial_no      AutoNumber 
emp_id      Number 
user_id      Text 
pwd       Text 
user_type      Text 

在此先感謝。

+0

請問這情況是有道理的。如果不con2.State = ConnectionState.Open然後con2.Open()? – DevelopmentIsMyPassion

+0

嘗試刪除條件,只寫con2.Open(),讓我知道 – DevelopmentIsMyPassion

+0

是的,我刪除如果codition,並存在相同的問題 –

回答

0

你可以嘗試執行此代碼,只是讓我知道,如果它的工作原理

Private Sub btnCreateUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateUser.Click 
    Dim con2 As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\OfficeAutomationSystem.accdb; Persist Security Info=False") 
    Dim cmd As New OleDb.OleDbCommand 
    If Not con2.State = ConnectionState.Open Then 
     con2.Open() 
    End If 
    cmd.Connection = con2 
    'add data to table 
    cmd.CommandText = "INSERT INTO tblUser(emp_id, user_id, pwd, user_type) " & _ 
         " values(" & Convert.ToInt32(Me.cbEmpId.SelectedItem) & ", '" & Me.txtUserId.Text & "','" & _ 
         Me.txtPassword.Text & "', '" & Me.cbUserType.SelectedItem & "')" 
    cmd.ExecuteNonQuery() 
    MsgBox("running") 
End Sub 
+0

此消息顯示在消息框中:正在運行 –

+0

我問我的代碼是否工作?你在我的答案中嘗試過代碼嗎? – DevelopmentIsMyPassion

+0

是的,我嘗試過,同樣的問題仍然存在 –

相關問題