Private Sub Contestant_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim command As String
Dim dsSET As New DataSet
Dim connect As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\athan\Documents\PAGEANT.accdb")
command = "SELECT * from Contestant "
Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(command, connect)
comSTR = "SELECT * from Contestant "
dsSET.Clear()
da.Fill(dsSET, "contest")
dgvContestant.DataSource = dsSET
dgvContestant.DataMember = "contest"
End Sub
我不理解上面的代码,但是它仍然从数据库中获取数据并将其加载到datagridview。
下面是另一个代码,但抛出此错误:'未为命令对象设置命令文本。'
Private Sub Contestant_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dsSET As New DataSet
Dim connect As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\athan\Documents\PAGEANT.accdb")
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand
With cmd
.Connection = connect
.CommandText = "SELECT * from Contestant "
Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(command, connect)
.Connection.Open()
.ExecuteNonQuery()
da.Fill(dsSET, "contest")
dgvContestant.DataSource = "contest"
.Connection.Close()
End With
End Sub
在第二个代码段中,您正在设置CommandText
的cmd
,但没有设置CommandText
的command
。然后将command
传递到数据适配器。为什么首先要有两个命令对象?如果cmd
是您设置了CommandText
的命令对象,那么肯定应该是您传递到数据适配器中的命令对象。
连接对象会在您的应用程序和数据库之间建立连接。可以通过该连接执行SQL,并来回传递数据。
命令对象包含SQL代码以及(可选)该SQL的参数。命令始终与执行该命令的连接相关联。如果命令包含SELECT
语句,则可以调用ExecuteScalar
检索单个值,或调用ExecuteReader
检索零,一个或多个包含一个或多个列的记录。如果该命令不包含SELECT
语句,则可以调用ExecuteNonQuery
。
数据适配器基本上是一组最多四个用于执行CRUD操作的命令对象。调用Fill
时,将执行SelectCommand
以将数据检索到DataTable
中。当您调用Update
时,将根据需要执行InsertCommand
,UpdateCommand
和DeleteCommand
,以将更改从DataTable
保存到数据库。
创建数据适配器时,可以为SelectCommand
提供现成的命令对象,也可以让适配器自己创建一个。如果执行后者,则可以传递SQL代码和现有连接,也可以传递SQL代码和连接字符串,在这种情况下,适配器也将创建连接对象。数据适配器不会创建自己的InsertCommand
,UpdateCommand
和DeleteCommand
,因此您必须自己创建它们,或者在某些情况下,您可以使用命令生成器来为其创建。
您可能会受益于我的ADO.NET示例here。
命令只是sql语句和关联连接的表示。它可以通过多种方式执行,方法是:返回带有.ExecuteReader的阅读器,对于带有.ExecuteNonQuery的Insert,Update和Delete语句,以及通过.ExecuteScalar检索单个值。 DataAdapter也可以使用它。
DataAdapter不仅可以填充数据表或数据集,还可以填充.Update
符合评论和解释。
Private Sub Contestant_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Declares a variable as String
Dim command As String
'Creates a DataSet object. Note the New keyword
Dim dsSET As New DataSet
'Creates a Connecion object and sets the .ConnectionString property by passing it to the Constructor of the object
Dim connect As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\athan\Documents\PAGEANT.accdb")
'Assigns a value to the previously declared String
command = "SELECT * from Contestant "
'Creates a DataAdapter object and provides an Sql Select statement that the adapter can use to create its SelectCommand property
'and sets the .Connection property by passing a Connection object.
'Note: the connection is NOT an open connection
Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(command, connect)
'Undeclared and unnecessary variable
'comSTR = "SELECT * from Contestant "
'Unnecessary code - You just created, it is already empty
'dsSET.Clear()
'Calls the DatAdapter .Fill method passing the DataSet to fill and the name of the DataTable being filled.
'The .Fill method opens and closes the connection if it finds it closed. If the connection is already open
'the .Fill method leaves it open.
da.Fill(dsSET, "contest")
'The DataSet is set as DataSourd
dgvContestant.DataSource = dsSET
'Since a DataSet can contain more than one table; the .DataMember of the DataSet
'is set to the name of the DataTable to display.
dgvContestant.DataMember = "contest"
End Sub
Private Sub Contestant_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dsSET As New DataSet
Dim connect As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\athan\Documents\PAGEANT.accdb")
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand
With cmd
.Connection = connect
.CommandText = "SELECT * from Contestant "
'Here command is not declared
'Visual Studion assumes you mean Interaction.Command() which is NOT at all want you want
Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(command, connect)
.Connection.Open()
'A DataAdapter does not have a .ExecuteNonQuery method
'.ExecuteNonQuuery belongs to .Command and is used for sql Statements that
'begin with Insert, Update or Delect.
.ExecuteNonQuery()
da.Fill(dsSET, "contest")
'The .DataSoure of a DataGridView cannot be set to a String
dgvContestant.DataSource = "contest"
.Connection.Close()
End With
End Sub
'I don't think you need a DataAdapter or a DataSet
Private Sub FillDataGridView()
Dim dt As New DataTable
Using cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\athan\Documents\PAGEANT.accdb"),
cmd As New OleDbCommand("SELECT * from Contestant ", cn)
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using
'Update the User Interface after the connection is closed.
dgvContestant.DataSource = dt
End Sub