我只想知道这两个在vb.net的区别?使用数据适配器和使用 oledb 命令?

I just want to know the difference of these two in vb.net? Using Data adapter and Using the oledb command?

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。

下面是另一个代码但抛出了这个错误: 'Command text was not set for the command object.'

  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

在第二个代码片段中,您设置了 cmdCommandText,但没有设置 commandCommandText。然后将 command 传递给数据适配器。为什么你首先有两个命令对象?如果 cmd 是您设置其 CommandText 的命令对象,那么它肯定应该是您传递给数据适配器的命令对象。

连接对象在您的应用程序和数据库之间创建连接。 SQL 可以通过该连接执行并来回传递数据。

命令对象包含 SQL 代码和可选的 SQL 参数。命令总是与执行它的连接相关联。如果命令包含 SELECT 语句,那么您可以调用 ExecuteScalar 来检索单个值或调用 ExecuteReader 来检索零个、一个或多个包含一列或多列的记录。如果命令不包含SELECT语句,则可以调用ExecuteNonQuery.

数据适配器基本上是一组最多四个执行 CRUD 操作的命令对象。当您调用 Fill 时,将执行 SelectCommand 以将数据检索到 DataTable 中。当您调用 Update 时,InsertCommandUpdateCommandDeleteCommand 会根据需要执行,以将 DataTable 中的更改保存到数据库中。

创建数据适配器时,您可以为 SelectCommand 提供现成的命令对象,也可以让适配器自己创建一个。如果您执行后者,则可以传递 SQL 代码和现有连接,或者您可以传递 SQL 代码和连接字符串,在这种情况下,适配器也会创建连接对象。数据适配器不会创建自己的 InsertCommandUpdateCommandDeleteCommand,因此您必须自己创建它们,或者在某些情况下,您可以使用命令生成器为您创建。

看看我的 ADO.NET 示例 here

您可能会受益

命令只是 SQL 语句和关联连接的表示。它可以通过多种方式执行,通过使用 .ExecuteReader 返回 reader,对于使用 .ExecuteNonQuery 的插入、更新和删除语句,以及使用 .ExecuteScalar 检索单个值。 =20=]

它也可以被DataAdapter使用。

一个DataAdapter不仅可以.Fill一个DataTableDataSet还可以.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 Connection 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 a 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
        '.ExecuteNonQuery belongs to .Command and is used for SQL statements that
        'begin with Insert, Update or Delete.
        .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