简体   繁体   中英

How do I create a dataset in VB.NET from this code?

I currently have the following code in my project which populates a DataGridView object with the results of an SQL query.

Sub PerformQuery(ByVal SQLText As String)
    Dim DbConnection As New OleDb.OleDbConnection(createConnectionString)

    Dim SQLQuery As String = SQLText

    Dim Adapter As New OleDb.OleDbDataAdapter(SQLQuery, DbConnection)

    Try
        Using Table As New DataTable
            Adapter.Fill(Table)
            Table.Locale = Globalization.CultureInfo.InvariantCulture
            DbConnection.Close()
            DataGridView1.DataSource = Table
        End Using
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Elsewhere in my project I can create a DataSet object using the code

Dim ds As New DataSet

And then extract data from it using code like:

MaxRows = ds.Tables("Dataset_Users").Rows.Count

Rather than populating a DataGridView, how can I use the PerformQuery code to create a dataset?

Thank you in advance for your help.

I think you are after the following:

Try
    Dim ds As New DataSet
    Using Table As New DataTable
        Adapter.Fill(Table)
        Table.Locale = Globalization.CultureInfo.InvariantCulture
        DbConnection.Close()
        DataGridView1.DataSource = Table
        ds.Table.Add(Table)
    End Using
Catch ex As Exception
    MsgBox(ex.Message)
End Try

Or as in your example you was after the Number of rows in the dataset you can do the same with the DataTable, For Example:

Try
    Dim MaxRows As Integer
    Using Table As New DataTable
        Adapter.Fill(Table)
        Table.Locale = Globalization.CultureInfo.InvariantCulture
        DbConnection.Close()
        DataGridView1.DataSource = Table

        '' Getting the number of rows in the DataTable
        MaxRows = Table.Rows.Count
    End Using
Catch ex As Exception
    MsgBox(ex.Message)
End Try

Think in a more functional style. Return the table instead of setting to grid. While we're here, let's update the method so you don't have to write queries anymore that leave you wide open to sql injection attacks:

Function PerformQuery(ByVal SQLText As String, ByVal ParamArray Parameters() As OleDbParameter) As DataTable
    Dim result As New DataTable()

    Using cn As New OleDb.OleDbConnection(createConnectionString), _
          cmd As New OleDb.OleDbCommand(SQLText, cn), _
          Adapter As New OleDb.OleDbDataAdapter(cmd, cn)

        If Parameters IsNot Nothing AndAlso Parameters.Length > 0 Then
            cmd.Parameters.AddRange(Parameters)
        End If
        Adapter.Fill(result)
    End Using

    Return Result
End Function

Note that I also removed the error handling and locale code. You still need to do that stuff, but when you want to just return a datatable instead of interact directly with the user interface in a method you have effectively moved your code to a lower level of abstraction. When you do that, you probably don't want to deal with the error handling at this lower level any more; instead let the exceptions bubble up where you can handle them closer to the user interface.

Now you call the updated method like this:

Dim sql As String = "SELECT * FROM Customers WHERE CustomerID = ?"
Dim CustID As New OleDb.OleDbParameter("CustomerId", OleDbType.Integer)
CustID.Value = 123456

Try
    DataGridView1.DataSource = PerformQuery(sql, CustID)
Catch Ex As Excpetion
    MsgBox(Ex.Message)
End Try

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM