Solved

Replace dataadapter by sql datareader

Posted on 2016-08-15
6
31 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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