Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 935
  • Last Modified:

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

0
AlHal2
Asked:
AlHal2
  • 3
  • 3
1 Solution
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
PlatoConsultantCommented:




         DataTable dtF2= new DataTable();
        dtF2.Load(myReader);
       
       Grid2.DataSource = dtF2
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now