none
How can I retrieve data from SQL to Visual Basic? RRS feed

  • Question

  • I need to retrieve some data that is obtained via SQL and get that info and pass it to Visual Basic, how can I do that?
    Wednesday, August 30, 2017 8:49 PM

All replies

  • There are several directions where the links point to code samples you can download, run the SQL scripts to generate the database and tables then try them out.

    • Using SqlClient managed data provider, I have a intermediate to advance MSDN code sample to try.
    • Using Microsoft Entity Framework, I have a MSDN code sample also.
    • There is also using data wizards that come in Visual Studio. I mention this but don't advise using them


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, August 30, 2017 9:01 PM
    Moderator
  • Assuming you mean SQL Server. There is a tutorial at the below link:

    https://www.tutorialspoint.com/vb.net/vb.net_database_access.htm

    If you have more specific questions then please post a follow-up.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, August 30, 2017 9:11 PM
  • Hi DR_RD,

    Here is the sample that retrieving data from SQL to vb.net.

     Dim ds As New DataSet
            Dim str As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C# and VB Support\Example(VB)\Demo\Data3.mdf;Integrated Security=True;Connect Timeout=30"
            Dim conn As New SqlConnection(str)
            conn.Open()
            Dim sql As String = "select count(Location) as LocationCount, Location as LocationName from Test11 group by Location"
            Dim cmd As New SqlCommand(sql, conn)
            Dim adapter As New SqlDataAdapter(cmd)
            'fill data into dataset
            adapter.Fill(ds, "TestTable")
            conn.Close()

    In general, we will use the datagridview control to display the data from the sql database, like this:

    http://vb.net-informations.com/datagridview/vb.net_datagridview_binding.htm

    Best Regards,

    Cherry



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 31, 2017 6:53 AM
    Moderator