我想從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
ü可以試試這個'sqlCom.CommandText =「INSERT INTO [員]([會員名],[會員姓氏],[兩性],[聯繫人],[電子郵件])「_ &」VALUES(@FirstName,@LastName,@Gender,@Contact,@Email);「'..在兩個語句末尾添加分號 – Olivarsham
爲什麼在'AddWithValue'中沒有'@'的參數,爲什麼你還沒有添加參數@ @ MembershipType' – Olivarsham
@Olivarsham如果你回顧我的問題,我說我已經嘗試添加分號。但是,我得到了''在SQL語句結束後發現的字符。「'。 – Gideon