Can I populate datagridview from data reader rather than data adapter

I'm using this code to popluate a datagridview from a database.
There can be about 40000 rows.  Is there a more efficient way of doing this eg using a data reader instead of an adapter?
dim con1 As New SqlClient.SqlConnection()
Dim dtF2 As New DataTable
Dim cmd1 As New SqlClient.SqlCommand

connectionString = "XXXX"
con1.ConnectionString = connectionString
con1.Open()
cmd1.Connection = con1
cmd1.CommandText = "StoredProcName"
cmd1.CommandType = CommandType.StoredProcedure
cmd1.Parameters.Clear()
cmd1.ExecuteNonQuery()
adapter.SelectCommand = cmd1
adapter.Fill(dtF2)
Grid2.DataSource = dtF2

Open in new window

AlHal2Asked:
Who is Participating?
 
PlatoConsultantCommented:




         DataTable dtF2= new DataTable();
        dtF2.Load(myReader);
       
       Grid2.DataSource = dtF2
0
 
PlatoConsultantCommented:
yes you can
Imports System.Data.SqlClient
Public Class Form1 Inherits System.Windows.Forms.Form
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim dr As New SqlDataReader()
'declaring the objects

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MyBase.Load
myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
'establishing connection. you need to provide password for sql server
Try
myConnection.Open()
'opening the connection
myCommand = New SqlCommand("Select * from discounts", myConnection)
'executing the command and assigning it to connection 
dr = myCommand.ExecuteReader()
While dr.Read()
'reading from the datareader
MessageBox.Show("discounttype" & dr(0).ToString())
MessageBox.Show("stor_id" & dr(1).ToString())
MessageBox.Show("lowqty" & dr(2).ToString())
MessageBox.Show("highqty" & dr(3).ToString())
MessageBox.Show("discount" & dr(4).ToString())
'displaying the data from the table
End While
dr.Close()
myConnection.Close()
Catch e As Exception
End Try
End Sub

Open in new window

0
 
PlatoConsultantCommented:
Calling stored procedure via data reeader
Example:
Dim SQLCon As New SqlClient.SqlConnection
SQLCon.ConnectionString = "Data Source=Server;User ID=User;Password=Password;"
SQLCon.Open()

Calling Stored Procedures with Exec command

SQLCmd.CommandText = "Exec SelectRecords 'Test', 'Test', 'Test'" 
SQLCmd.Connection = SQLCon 'Active Connection

Open in new window

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
AlHal2Author Commented:
How do I link the datagridview to the datareader?
Also I put in "Dim dare As New SqlClient.SqlDataReader" and got a message that it has no constructors.
"Dim cmd1 As New SqlClient.SqlCommand" does not get this message.
0
 
AlHal2Author Commented:
If I have to use a data table anyway, am I better off with using the adapter as per my original code?
0
 
AlHal2Author Commented:
Thanks.  I can work with this.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.