我正在尝试让我的系统将组合框值插入到我的访问数据库中。每当我尝试单击“添加”按钮时,我总会得到这个很长的错误,而且我感觉这是因为我的INSERT语句。这是我表格的全部代码。任何帮助将不胜感激!谢谢
Imports System.Data.OleDb
Public Class AdmMain
Sub fillcombo()
strsql = " select yrgr from yearandgrade"
Dim acscmd As New OleDb.OleDbCommand
acscmd.CommandText = strsql
acscmd.Connection = acsconn
acsdr = acscmd.ExecuteReader
While (acsdr.Read())
cboyr.Items.Add(acsdr("yrgr"))
End While
acscmd.Dispose()
acsdr.Close()
End Sub
Sub comb2()
strsql = " select sections from sectio"
Dim acscmd As New OleDb.OleDbCommand
acscmd.CommandText = strsql
acscmd.Connection = acsconn
acsdr = acscmd.ExecuteReader
While (acsdr.Read())
cbosec.Items.Add(acsdr("sections"))
End While
acscmd.Dispose()
acsdr.Close()
End Sub
Private Sub LinkLabel1_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
If MessageBox.Show("Are you sure you want to logout?", "Logout", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
MessageBox.Show("You have successfully logged out of VCM's Library Information System!", "Logout Confirmed")
Me.Close()
LoginUser.Show()
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Me.txtFName.Text = ""
Me.txtMName.Text = ""
Me.txtLName.Text = ""
Me.cboyr.Text = ""
Me.cbosec.Text = ""
Me.txtFName.Focus()
End Sub
Private Sub AdmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Module1.connect()
Me.fillcombo()
Me.comb2()
End Sub
Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
Dim rbdtext As String = cboyr.SelectedItem.ToString
Dim uno As String = cbosec.SelectedItem.ToString
Try
Using conn = New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Thesis\Thesis\Database1.accdb"
conn.Open()
Dim sqlquery As String = "INSERT INTO students (StudentID, FirstName,MiddleName,LastName,Yr, Section) " & _
"VALUES (@studid, @fname,@mname,@lname,@yr, @sec)"
Dim SqlCommand As New System.Data.OleDb.OleDbCommand
SqlCommand.Parameters.AddWithValue("@studid", TxtID.Text)
SqlCommand.Parameters.AddWithValue("@fname", txtFName.Text)
SqlCommand.Parameters.AddWithValue("@mname", txtMName.Text)
SqlCommand.Parameters.AddWithValue("@lname", txtLName.Text)
SqlCommand.Parameters.AddWithValue("@yr", rbdtext)
SqlCommand.Parameters.AddWithValue("@sec", uno)
SqlCommand.Connection = conn
Dim sqlRead As System.Data.OleDb.OleDbDataReader = SqlCommand.ExecuteReader()
MsgBox("One record successfully added!", "Added!")
End Using
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
继续显示btw的错误! http://i.imgur.com/DgjiWqm.png
答案 0 :(得分:1)
您似乎永远不会在SqlCommand
方法中为btnAdd_Click
分配select语句。尝试添加SqlCommand.CommandText = sqlquery
。
答案 1 :(得分:0)
您需要以这种方式更改查询
Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
Dim rbdtext As String = cboyr.SelectedItem.ToString
Dim uno As String = cbosec.SelectedItem.ToString
Try
Dim cnString = = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\Thesis\Thesis\Database1.accdb"
Dim sqlquery As String = "INSERT INTO students " & _
"(StudentID, FirstName,MiddleName,LastName,Yr, [Section]) " & _
"VALUES (@studid, @fname,@mname,@lname,@yr, @sec)"
' Use this form to initialize both connection and command to
' avoid forgetting to set the appropriate properties....
Using conn = New System.Data.OleDb.OleDbConnection(cnString)
Using cmd = New System.Data.OleDb.OleDbCommand(sqlQuery, conn)
conn.Open()
cmd.Parameters.AddWithValue("@studid", TxtID.Text)
cmd.Parameters.AddWithValue("@fname", txtFName.Text)
cmd.Parameters.AddWithValue("@mname", txtMName.Text)
cmd.Parameters.AddWithValue("@lname", txtLName.Text)
cmd.Parameters.AddWithValue("@yr", rbdtext)
cmd.Parameters.AddWithValue("@sec", uno)
Dim rowsInserted = cmd.ExecuteNonQuery()
if rowsInserted > 0 Then
MessageBox.Show("One record successfully added!", "Added!")
else
MessageBox.Show("Failure to add new record!", "Failure!")
End if
End Using
End Using
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
我已经更改了OleDbCommand对象的名称,以避免与SqlClient命名空间中使用的类SqlCommand产生不必要的混淆(不是严格必要但在阅读代码时却令人困惑)。然后我使用OleDbCommand构造函数来获取命令文本和命令使用的连接。这样可以避免忘记设置这些基本属性,最后 SECTION 是reserved keyword for MS-Access,因此,在查询文本中使用时,需要将其封装在方括号之间,否则会得到{{1} }