none
Get a value from a database in VIsual Studio RRS feed

  • Question

  • Hello everyone.

    I am using visual studio and programming in VB, and now I am interested in implementing a database to my solutions.

    Thus fur, what I have done is the creation of a connection

    Dim connectionString As String = "Data Source=C:\Users\WALTER\Documents\Visual Studio 2017\Projects\database_try\database_try2.sdf"

    Dim cn As SqlClient.SqlConnection

    cn = New SqlClient.SqlConnection(connectionString)

     

    The problem is I have looked and searched on the web about the way to get a value of a certain variable created in a table in a simple manner, but I do not get it.

    In a simple way, If I crate a table

    Name     Number

    Walter     123

    What I want to know is: How do I get the name or the number from the previously created table, and save it in a variable.?

    Thanks in advance.

    Tuesday, May 16, 2017 3:07 PM

All replies

  • Hi Walter!

    Following these steps : 

    • Create your connection string
    Dim connectionString As String = "Data Source=localhost;........."

    • Connect to your Database
    Dim connection As New SqlConnection(connectionString)
    conn.Open()


    • Create a Command and the query
    Dim command As New SqlCommand("SELECT * FROM Client", connection)
    Dim reader As SqlDataReader = command.ExecuteReader()  //Execute the Query


    • Retrieve your result. There are several ways
    Dim dt As New DataTable()
    dt.Load(reader)

    • Close the connection
    connection.Close()

    For more info : http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx

    Hope it will be useful.

    Please remember to mark the replies as answers if they help , this will help others who are looking for solutions to the same or similar problem. 


    Download my NEW FREE application MCP PREPARATION in the Windows 10 Store for preparing your Microsoft Certification or test your skills in Microsoft Technologies;



    Tuesday, May 16, 2017 3:40 PM
  • Hi Walter David Sanchez,

    Welcome to the MSDN forum.

    Refer to your description, your issue is about the VB development. Since our forum is to discuss the VS IDE, i will help you move this thread to the appropriate forum: Visual Studio LanguagesVisual Basic to seek for a more professional support, thank you for your understanding.

    Best regards,

    Sara


    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.

    Wednesday, May 17, 2017 1:40 AM
  • Hi Walter,

    You need to use SqlDataReader to get value from DataBase, you can refer to the code below. I put one Button and TextBox control in the form.

    Private Sub Loaddate()
            Dim firstname As String
            Dim strConn As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C# and VB Support\Example(VB)\Demo\Data3.mdf;Integrated Security=True"
            Using conn As New SqlConnection(strConn)
                Dim selectSql = "select firstname from test1 where id=@ID"
                Using com As New SqlCommand(selectSql, conn)
                    Try
                        conn.Open()
                        com.Parameters.AddWithValue("@ID", TextBox1.Text.Trim())
                        Dim datareader As SqlDataReader = com.ExecuteReader
                        While datareader.Read()
                            firstname = datareader(0)
                            MessageBox.Show(firstname)
                        End While
                        conn.Close()
                    Catch ex As Exception
                        MessageBox.Show(ex.ToString())
                    End Try
                End Using
            End Using
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Loaddate()
        End Sub

    Hope it is helpful to you.

    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.

    Wednesday, May 17, 2017 7:33 AM
    Moderator
  • Walter,

    Strange enough tell many persons here in this forum always to use a datareader to get a value while for that is the Scalar.

    Look here at sample code on our website which is almost likewise your database.

    http://www.vb-tips.com/ExecuteScalarText.ASPX 


    Success
    Cor

    Wednesday, May 17, 2017 7:59 AM