在尝试将一些信息添加到我的访问数据库时,我一直坚持这种编码方式
这是我的编码:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.Text & TextBox2.Text & TextBox3.Text & TextBox4.Text & TextBox5.Text & TextBox6.Text & TextBox7.Text = Nothing Then
MsgBox("Please fill up all information.")
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
TextBox7.Clear()
Else
Try
Dim myConnection As OleDbConnection
myConnection = New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=G:\Sem7 (Final Project)\Bookstore POS System\possys.mdb;Persist Security Info=false;")
Dim myCommand As OleDbCommand
myCommand = New OleDbCommand("INSERT INTO customer([ID],[c_name],[email],[phone],[nric],[sex],[race]) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "'','" & TextBox3.Text & "'','" & TextBox4.Text & "'','" & TextBox5.Text & "'','" & TextBox6.Text & "'','" & TextBox7.Text & "')", myConnection)
Dim id As New OleDbParameter("@ID", OleDbType.VarChar)
Dim name As New OleDbParameter("@c_name", OleDbType.VarChar)
Dim email As New OleDbParameter("@email", OleDbType.VarChar)
Dim phone As New OleDbParameter("@phone", OleDbType.VarChar)
Dim nric As New OleDbParameter("@nric", OleDbType.VarChar)
Dim sex As New OleDbParameter("@sex", OleDbType.VarChar)
Dim race As New OleDbParameter("@race", OleDbType.VarChar)
id.Value = TextBox1.Text
name.Value = TextBox2.Text
email.Value = TextBox3.Text
phone.Value = TextBox4.Text
nric.Value = TextBox5.Text
sex.Value = TextBox6.Text
race.Value = TextBox7.Text
myCommand.Parameters.Add(id)
myCommand.Parameters.Add(name)
myCommand.Parameters.Add(email)
myCommand.Parameters.Add(phone)
myCommand.Parameters.Add(nric)
myCommand.Parameters.Add(sex)
myCommand.Parameters.Add(race)
myCommand.Connection.Open()
myCommand.ExecuteNonQuery()
myCommand.Dispose()
MsgBox("Account Created", MsgBoxStyle.Information, "Registered!")
myConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
答案 0 :(得分:0)
Private Sub CreateAccount()
Try
Using myConnection As New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=G:\Sem7 (Final Project)\Bookstore POS System\possys.mdb;Persist Security Info=false;")
Using myCommand As New OleDbCommand("INSERT INTO customer([ID],[c_name],[email],[phone],[nric],[sex],[race]) VALUES (?,?,?,?,?,?,?)", myConnection)
myCommand.Parameters.Add("@ID", OleDbType.VarChar).Value = TextBox1.Text
myCommand.Parameters.Add("@c_name", OleDbType.VarChar).Value = TextBox2.Text
myCommand.Parameters.Add("@email", OleDbType.VarChar).Value = TextBox3.Text
myCommand.Parameters.Add("@phone", OleDbType.VarChar).Value = TextBox4.Text
myCommand.Parameters.Add("@nric", OleDbType.VarChar).Value = TextBox5.Text
myCommand.Parameters.Add("@sex", OleDbType.VarChar).Value = TextBox6.Text
myCommand.Parameters.Add("@race", OleDbType.VarChar).Value = TextBox7.Text
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
MsgBox("Account Created", MsgBoxStyle.Information, "Registered!")
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub