2016-02-26 99 views
0

我想從VB.NET插入數據到MS Access中的多個表,但我得到「SQL語句末尾缺少分號(;)」。來自MessageBox.Show(ex.Message)的。但是,一旦我添加了一個;,我收到此錯誤:「SQL語句結束後發現的字符」。VB.NET INSERT INTO ... VALUES聲明到多個表

我在哪裏做錯了?有沒有人可以自由地看看我的代碼,並幫助我解決我的問題?此外,如果你們有更好的替代編碼方式,我將非常感謝。提前致謝。

Imports System.Data.OleDb 
Public Class addNew 
Dim conn As New OleDbConnection() 
Dim sqlCom As New OleDbCommand() 

Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click 
If txtFirstName.Text = "" Or txtLastName.Text = "" Or txtContact.Text = "" Or txtEmail.Text = "" Or comboMembershipType.Text = "" Then 
    MessageBox.Show("Please complete the required fields.", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
Else 
    Try 
     conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Application.StartupPath & "\igcDatabase.accdb" 
     sqlCom.Connection = conn 
     conn.Open() 

     sqlCom.CommandText = "INSERT INTO [Members] ([Member First Name], [Member Last Name], [Gender], [Contact], [Email])" _ 
      & " VALUES (@FirstName, @LastName, @Gender, @Contact, @Email)" 
     sqlCom.CommandText &= "INSERT INTO [Membership Types] ([Membership Type])" & "VALUES (@MembershipType)" 


     Dim gender As String 
     If rbtnMale.Checked = True Then 
      gender = "Male" 
     Else 
      gender = "Female" 
     End If 

      sqlCom.Parameters.AddWithValue("@FirstName", txtFirstName.Text) 
      sqlCom.Parameters.AddWithValue("@LastName", txtLastName.Text) 
      sqlCom.Parameters.AddWithValue("@Gender", gender) 
      sqlCom.Parameters.AddWithValue("@Contact", txtContact.Text) 
      sqlCom.Parameters.AddWithValue("@Email", txtEmail.Text) 
      sqlCom.Parameters.AddWithValue("@MembershipType", comboMembershipType.Text) 

     sqlCom.ExecuteNonQuery() 
     MessageBox.Show("Successfully added member!") 
     conn.Close() 

    Catch ex As Exception 
     MessageBox.Show(ex.Message) 
    End Try 
End If 
End Sub 
End Class 

更新工作代碼:

Imports System.Data.OleDb 
Public Class addNew 
Dim conn As New OleDbConnection() 
Dim sqlCom As New OleDbCommand() 

Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click 
If txtFirstName.Text = "" Or txtLastName.Text = "" Or txtContact.Text = "" Or txtEmail.Text = "" Or comboMembershipType.Text = "" Then 
    MessageBox.Show("Please complete the required fields.", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
Else 
    Try 
     conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Application.StartupPath & "\igcDatabase.accdb" 
     sqlCom.Connection = conn 
     conn.Open() 

         sqlCom.CommandText = "INSERT INTO [Members] " _ 
            & "([Member First Name], [Member Last Name], [Gender], [Contact], [Email])" _ 
             & " VALUES (@FirstName, @LastName, @Gender, @Contact, @Email)" 



      Dim gender As String 
      If rbtnMale.Checked = True Then 
       gender = "Male" 
      Else 
       gender = "Female" 
      End If 

      'Parameter is used below to prevent SQL Injection 
      sqlCom.Parameters.AddWithValue("FirstName", txtFirstName.Text) 
      sqlCom.Parameters.AddWithValue("LastName", txtLastName.Text) 
      sqlCom.Parameters.AddWithValue("Gender", gender) 
      sqlCom.Parameters.AddWithValue("Contact", txtContact.Text) 
      sqlCom.Parameters.AddWithValue("Email", txtEmail.Text) 

      'Code below is used as the query does not return any data back to the form 
      sqlCom.ExecuteNonQuery() 

      'Query for Membership Types Table in MS Acess 
      sqlCom.CommandText = "INSERT INTO [Membership Types] ([Membership Type])" & " VALUES (@MembershipType)" 

      'Parameter is used below to prevent SQL Injection 
      'sqlCom.Parameters.Clear() is used to clear the previous input of data 
      sqlCom.Parameters.Clear() 
      sqlCom.Parameters.AddWithValue("MembershipType", comboMembershipType.Text) 

      'Code below is used as the query does not return any data back to the form 
      sqlCom.ExecuteNonQuery() 

      MessageBox.Show("Successfully added member!") 

      'Close Data Connection 
      conn.Close() 

    Catch ex As Exception 
     MessageBox.Show(ex.Message) 
    End Try 
End If 
End Sub 
End Class 
+0

ü可以試試這個'sqlCom.CommandText =「INSERT INTO [員]([會員名],[會員姓氏],[兩性],[聯繫人],[電子郵件])「_ &」VALUES(@FirstName,@LastName,@Gender,@Contact,@Email);「'..在兩個語句末尾添加分號 – Olivarsham

+0

爲什麼在'AddWithValue'中沒有'@'的參數,爲什麼你還沒有添加參數@ @ MembershipType' – Olivarsham

+0

@Olivarsham如果你回顧我的問題,我說我已經嘗試添加分號。但是,我得到了''在SQL語句結束後發現的字符。「'。 – Gideon

回答

1

試試這個

Imports System.Data.OleDb 
Public Class addNew 
Dim conn As New OleDbConnection() 
Dim sqlCom As New OleDbCommand() 

Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click 
If txtFirstName.Text = "" Or txtLastName.Text = "" Or txtContact.Text = "" Or txtEmail.Text = "" Or comboMembershipType.Text = "" Then 
MessageBox.Show("Please complete the required fields.", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
Else 
Try 
    conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Application.StartupPath & "\igcDatabase.accdb" 
    sqlCom.Connection = conn 
    conn.Open() 

    sqlCom.CommandText = "INSERT INTO [Members] ([Member First Name], [Member Last Name], [Gender], [Contact], [Email])" _ 
     & " VALUES (@FirstName, @LastName, @Gender, @Contact, @Email)"   

    Dim gender As String 
    If rbtnMale.Checked = True Then 
     gender = "Male" 
    Else 
     gender = "Female" 
    End If 

     sqlCom.Parameters.AddWithValue("@FirstName", txtFirstName.Text) 
     sqlCom.Parameters.AddWithValue("@LastName", txtLastName.Text) 
     sqlCom.Parameters.AddWithValue("@Gender", gender) 
     sqlCom.Parameters.AddWithValue("@Contact", txtContact.Text) 
     sqlCom.Parameters.AddWithValue("@Email", txtEmail.Text) 
sqlCom.ExecuteNonQuery() 


sqlCom.CommandText = "INSERT INTO [Membership Types] ([Membership Type])" & "VALUES (@MembershipType)" 

sqlCom.Parameters.Clear() 
     sqlCom.Parameters.AddWithValue("@MembershipType", comboMembershipType.SelectedText) 

    sqlCom.ExecuteNonQuery() 
    MessageBox.Show("Successfully added member!") 
    conn.Close() 

Catch ex As Exception 
    MessageBox.Show(ex.Message) 
End Try 
End If 
End Sub 
End Class 
+0

感謝您的幫助,非常感謝。但是,我仍然在SQL語句末尾丟失了分號(;)。 – Gideon

+0

嘗試將命令類型設置爲文本。 sqlCom.CommandType = System.Data.CommandType.Text –

+0

問題修復。非常感謝你! – Gideon

0

有一個在Addwithvalue 沒有@應該是sqlCom.Parameters.AddWithValue("@Email", txtEmail.Text);

不知道這一點,但我的工作在C#和語法像 sqlCom .CommandText =「INSERT INTO [Members]([Member First Name],[Member Last Name],[Gender],[Contact],[Email])VALUES(@FirstName,@LastName,@Gender,@Contact,@Email )「在C#中肯定有關VB

並嘗試使用存儲過程,它使代碼整潔,工作容易,並且防止SQL注入這是一個重大的安全問題。

,你也沒有添加性別的價值的SqlCommand,用參數

+0

'@'在Parameters.Add函數中不是必需的。即使我加了'@',我面臨的問題仍然存在。 – Gideon

+0

http://stackoverflow.com/questions/11139791/how-to-use-parameters-in-an-sql-command-in-vb 檢查了這一點@是必要的 並且你試着用它來查詢我寫的嗎? 並且您還沒有將性別值添加到sqlcommand中,請將其與參數 –

+0

Opps一起傳遞,對於錯字感到抱歉。我確實在我最新的VB.NET項目中包含了這兩個項目。剛剛更新了有問題的代碼。感謝您提供的鏈接,我已經添加了「@」,但問題仍然存在。 – Gideon

1

你所做的是連接兩個字符串通過它 - &不是空格。擺脫&和第二個查詢中的引號,並且只有membershiptype])VALUES ...然後在第一個查詢的末尾添加一個空格,否則它會讀取...... @email)INSERT ... membershiptype] )值。你也嘗試添加一個;第一次查詢後,但不是第二次後?

+0

它可能不算什麼 - 但是代碼中的membershiptype]和VALUES之間沒有空格 - 它可能錯誤地讀取了這行。嘗試在那裏添加一個空間,並在第一個查詢結束時(所以兩個查詢之間有一個空格) – ninjaRoche

+0

我不太明白你想告訴我什麼。我以爲我確實在'membershiptype])'和'VALUES'之間加了一個空格?將不勝感激您可以在修復後爲我提供代碼。提前致謝。 – Gideon

+0

你所做的是連接兩個字符串 - &不是空格。刪除第二個查詢中的&和引號,並且只有membershiptype])VALUES ...然後在第一個查詢的末尾添加一個空格,否則它會讀取...... @email)INSERT ... membershiptype] )值。你也嘗試添加一個;第一次查詢後,但不是第二次後? – ninjaRoche