2014-03-12 46 views
0

有人請幫我找到我的代碼上的錯誤。 錯誤是在我的嘗試線內,並試圖在我的數據庫訪問添加記錄。錯誤是「Syntax error in INSERT into statement」。我已經嘗試使用添加到訪問數據庫時INSERT語句中的語法錯誤

ds.Tables("Users").Rows.Add(dsNewRow) 
      da.Update(ds, "Users") 

在我的選民登記,它工作正常。 idk爲什麼它不適用於這種形式(用戶註冊)。

Imports System.Data.OleDb 

Public Class UserRegister 
Dim con As New OleDb.OleDbConnection 
Dim dbProvider As String 
Dim dbSource As String 
Dim ds As New DataSet 
Dim da As New OleDb.OleDbDataAdapter 
Dim sql As String 


Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click 

End Sub 

Private Sub UserRegister_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    dbProvider = " PROVIDER=Microsoft.jet.OLEDB.4.0;" 
    dbSource = "Data Source= C:\Users\Ronel\Documents\database\CSdatabase.mdb" 

    con.ConnectionString = dbProvider & dbSource 
    con.Open() 
    sql = "SELECT*FROM tblUsers" 
    da = New OleDb.OleDbDataAdapter(sql, con) 
    da.Fill(ds, "Users") 
    MsgBox("Database now Open") 
    con.Close() 
    'MsgBox("Database now Close") 

End Sub 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim cb As New OleDb.OleDbCommandBuilder(da) 
    Dim dsNewRow As DataRow 

    Dim empty = 
    Me.Controls.OfType(Of TextBox)().Where(Function(txt) txt.Text.Length = 0) 
    If empty.Any Then 
     MessageBox.Show(String.Format("PLEASE FILL ALL FIELDS:")) 
    Else 
     dsNewRow = ds.Tables("Users").NewRow() 
     dsNewRow.Item("Username") = TextBoxUser.Text 
     dsNewRow.Item("Password") = TextBoxPass.Text 
     dsNewRow.Item("LastName") = TextBoxFN.Text 
     dsNewRow.Item("GivenName") = TextBoxGN.Text 
     dsNewRow.Item("MiddleName") = TextBoxMN.Text 

     ' Try 
     ds.Tables("Users").Rows.Add(dsNewRow) 
     da.Update(ds, "Users") 
     ' Catch ex As Exception 
     MsgBox("Error updating") 
     ' End Try 

     ' Me.Dispose() 
     'Comelec.Show() 
    End If 

End Sub 
End Class 
+0

嘗試'sql =「選擇*從tblUsers」'... – matzone

+0

試過這個。即時通訊仍然收到錯誤。 –

+0

是的。在我的訪問數據庫中,我有一些由管理員填寫的字段,而不是由用戶填寫。 –

回答

0

我看到一些問題。首先,你的SQL語句需要一些空格。而不是sql = "SELECT*FROM tblUsers",請使用sql = "SELECT * FROM tblUsers"

其次,使用OleDbCommandBuilder時,該代碼連接到使用爲DataAdapter提供的SELECT語句從數據庫,並使用它來生成更新所需的SQL,刪除和插入語句。與數據庫的連接需要打開才能發生 - 在CommandBuilder正在運行的位置關閉連接。這可能是語法錯誤來自的地方。

CommandBuilder將使用SELECT中指定的字段的所有創建這些語句。如果您的用戶表中的字段不是您嘗試填充的五個字段,則OleDbCommandBuilder仍將生成一個INSERT語句,該語句也會嘗試填充這些字段。根據您的表的結構,這可能會導致行被拒絕,如果必填字段未填充。要看看正在發生什麼樣的SQL語句,你可以看看DataAdapter對象的屬性使用CommandBuilder的後:

con.Open 
sql = "SELECT * FROM tblUsers" 
da = New OleDb.OleDbDataAdapter(sql, con) 
Dim cb As New OleDb.OleDbCommandBuilder(da) 
Debug.Print("SELECT: " & da.SelectCommand.CommandText) 
Debug.Print("UPDATE: " & da.UpdateCommand.CommandText) 
Debug.Print("DELETE: " & da.DeleteCommand.CommandText) 
Debug.Print("INSERT: " & da.InsertCommand.CommandText) 

上CommandBuilders一些額外的閱讀,請訪問:http://msdn.microsoft.com/en-us/library/tf579hcz%28v=vs.110%29.aspx

給這嘗試:

Imports System.Data.OleDb 

Public Class UserRegister 
    Const dbProvider As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" 
    Dim dbSource As String = "Data Source=C:\Users\Ronel\Documents\database\CSdatabase.mdb" 
    Dim con As New OleDb.OleDbConnection(dbProvider & dbSource) 
    Dim ds As New DataSet 
    Dim da As New OleDb.OleDbDataAdapter 
    'Specify only the fields you need 
    Dim sql As String = "SELECT UserName, Password, LastName, GivenName, MiddleName FROM tblUsers" 


    Private Sub UserRegister_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     con.Open() 
     da = New OleDb.OleDbDataAdapter(sql, con) 
     da.Fill(ds, "Users") 
     ' Get commands now, while connection is open. Do this once when the form is loaded, not every time button is clicked. 
     Dim cb As New OleDb.OleDbCommandBuilder(da) 
     con.Close() 
    End Sub 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     ' Validation code goes here 
     If empty.Any Then 
      MessageBox.Show(String.Format("PLEASE FILL ALL FIELDS:")) 
     Else 
      Dim dsNewRow As DataRow 
      dsNewRow = ds.Tables("Users").NewRow() 
      dsNewRow.Item("Username") = TextBoxUser.Text 
      dsNewRow.Item("Password") = TextBoxPass.Text 
      dsNewRow.Item("LastName") = TextBoxFN.Text 
      dsNewRow.Item("GivenName") = TextBoxGN.Text 
      dsNewRow.Item("MiddleName") = TextBoxMN.Text 

      Try 
       con.Open() 
       ds.Tables("Users").Rows.Add(dsNewRow) 
       da.Update(ds, "Users") 
      Catch ex As Exception 
       MsgBox("Error updating: " & Err.Description) 
      Finally 
       con.Close() 
      End Try 
     End If 
    End Sub 
End Class 
相關問題