2017-06-23 153 views
0

我正在爲Visual Basic程序創建一個簡單的用戶ID /密碼訪問數據庫。 除了添加新用戶之外,一切都可以使用。Visual Basic 2013在Access Execure中創建新記錄錯誤

我的文本框對象被鏈接到我的數據庫,但當我嘗試添加我的新用戶時,我不斷收到此錯誤。

Screen grab of Error window at .ExecuteNonQuery()

Error: An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

這裏是我的代碼:

Imports System.Data.OleDb 
Public Class newUser 

    Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\PasswordCheck.accdb" 
    Dim conn As OleDbConnection = New OleDbConnection 


    Private Sub btnNewUser_Click(sender As Object, e As EventArgs) Handles btnNewUser.Click 


     Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\PasswordCheck.accdb" 
     Dim conn As OleDbConnection = New OleDbConnection 


     conn.ConnectionString = connString 

     conn.Open() 

     Dim SaveNew As String = "INSERT INTO [Password] (UserId, Password, firstName, LastName) Values ('" & txtUserID.Text & "','" & txtPassword.Text & "','" & txtFirst.Text & "','" & txtLast.Text & "')" 
     Dim cmd As New OleDbCommand 
     cmd.Connection = conn 

     With cmd 
      .CommandText = SaveNew 
      .Connection = conn 
      .ExecuteNonQuery() 
     End With 
     MsgBox("You have been added to our Database") 
     txtUserID.Text = "" 
     txtPassword.Text = "" 
     txtFirst.Text = "" 
     txtLast.Text = "" 
     conn.Close() 

    End Sub 



    Private Function SaveNew() As String 
     Throw New NotImplementedException 
    End Function 

End Class 
+0

A)那是一個VB.NET代碼/錯誤不是VB6所以標記是錯誤的,b)我們需要錯誤的文本在帖子中,而不是一個錯誤圖片的鏈接c)有更多的信息詳細信息d)**不要**串聯字符串以生成SQL;使用sql參數e)密碼永遠不應該作爲純文本存儲 - 對它們進行哈希處理。 f)閱讀[問],參加[遊覽],修復標記和[編輯]帖子請 – Plutonix

+0

我同意Plutonix所說的一切。但是,如果你堅持使用字符串連接,你會錯過變量之間的逗號 – Simon

+0

我已經改變了標籤,並添加了逗號。我仍然得到錯誤。包含錯誤的文本爲 – Fazz1967withExplosions

回答

0

密碼是保留字也是一個字段,所以:

Dim SaveNew As String = "INSERT INTO [Password] (UserId, [Password], FirstName, LastName) Values ('" & txtUserID.Text & "','" & txtPassword.Text & "','" & txtFirst.Text & "','" & txtLast.Text & "')" 

而且進一步,如果用戶ID不就是文字,然後暫無報價:

Dim SaveNew As String = "INSERT INTO [Password] (UserId, [Password], FirstName, LastName) Values (" & txtUserID.Text & ",'" & txtPassword.Text & "','" & txtFirst.Text & "','" & txtLast.Text & "')" 

也就是說,嘗試探險家如何使用參數開展了這一點。比SQL連接更有趣。

+0

AH HUH!好。謝謝,古斯塔夫。我再次嘗試不同的字段名稱。不幸的是,我還沒有找到使用參數的例子。你的意思是這樣嗎? Dim cmd As OleDbCommand = New OleDbCommand(「SELECT * FROM [Passwords] WHERE [userID] ='」&txtUserID.Text&「'AND [Password] ='」&txtPassword.Text&「'」,myConnection) – Fazz1967withExplosions

+0

否。就像這樣:[OleDb Parameters](https://stackoverflow.com/questions/21924636/oledb-parameters/21924799#21924799)。但是 - 正如有人已經以自己的禮貌方式告訴你的 - 如果你瀏覽一下,你會發現很多例子和指南。我自己不使用OleDb的東西;我更喜歡使用_DataTableAdapter_,但這是另一回事。 – Gustav