Solved

Replace dataadapter by sql datareader

Posted on 2016-08-15
6
20 Views
Last Modified: 2016-08-16
Hello,
Is there any way I can replace the data adapter with sql reader to fill datatable.

 Dim SQLobj As New SqlCommand("L_Sy")
  SQLobj.Parameters.AddWithValue("@Title", Dy_Title)
            SQLobj.Parameters.AddWithValue("@Year_Ref1", Dy_Year_Ref1)
            SQLobj.Parameters.AddWithValue("@Year_Ref2", Dy_Year_Ref2)
            SQLobj.Parameters.AddWithValue("@FaxNumber", Dy_FaxNumber)
            SQLobj.Parameters.AddWithValue("@Attn", Dy_Attn)

    SQLobj.CommandType = CommandType.StoredProcedure
            SQLobj.Connection = sql.GetConnection
            clsFrmmain.Populate(SQLobj)



  Public Sub Populate(ByVal Command As SqlCommand)
        'Accept command as string and fill the datatable with the result of sql query
        ' Test_Str = "Test"
        Dim dt = New DataTable
        Try
            adapter = New SqlDataAdapter()
            adapter.SelectCommand = Command
            dt.BeginLoadData()
            adapter.Fill(dt)
            dt.EndLoadData()
            Command.Connection.Close()
            Command.Dispose()
            Data_Table = dt


        Catch MyException As SqlException
            MessageBox.Show("Stored procedure Error: MySQL code: " &
            MyException.Number & "  " &
            MyException.Message)
        End Try
    End Sub
0
Comment
Question by:RIAS
  • 3
  • 3
6 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41756442
Yes.  For each row in the reader add a new row to the data table and copy the contents of each field from the reader into the equivalent field in the new row in the table.

ps.  This is rather more code for you to write an could possibly run more slowly as well.
0
 

Author Comment

by:RIAS
ID: 41756450
I read an article which prompts it will be faster:

var dataReader = cmd.ExecuteReader();
var dataTable = new DataTable();
dataTable.Load(dataReader);
0
 

Author Comment

by:RIAS
ID: 41756452
Need to know how I can implement here.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41756471
You're right.  The Load method can use a datareader as the source (guess who hasn't ever done that).
In that case it is probably faster than using an adapter (a datareader is a lightweight class supporting limited functionality) but make certain the table and reader do match one another in terms of the columns of data.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 41756476
0
 

Author Comment

by:RIAS
ID: 41756507
Andy,
Any suggestion in my code
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now