Here are some tips, sample codes, problem solving for everything regarding visual basic (VB) and sql server (MS SQL). Some are How-to's and syntax of VB and sql. These are solutions that I researched and compiled while I'm developing my program using VB and SQL. Hope this helps you in your programming skills.

Wednesday, April 7, 2010

How To Retrieve a Record in SQL using VB



Below is a sample code on how you can retrieve one record in MS SQL using Visual Basic.  This is just one way of doing it.

--------------------------------------------------------------------------------------
Dim SQLStr As String = "SELECT * FROM Master where code =" + txtcode.text

        Using connection As New SqlConnection(ConnectionString)
            Dim SQLCmd As New SqlCommand(SQLStr, connection)
            connection.Open()
            Dim SQLdr As SqlDataReader = SQLCmd.ExecuteReader()
            If SQLdr.Read()
                Txtname.text = (SQLdr("name").ToString)
            End If
            SQLdr.Close()
            'no need to close connection here
        End Using

----------------------------------------------------------------------------------------