Visual Basic Code Snippet - Get Data From SqlDataReader

Visual Basic Code Snippet - Get Data From SqlDataReader

Visual Basic Code Snippet - Get Data From SqlDataReader

(VB) Visual Basic code snippet connects to SQL server and executes SQL statement and return SqlDataReader. GetSqlDataReader returns SqlDataReader for given database connection string and SQL statement.

Bookmark:

Visual Basic Code Snippet - Get Data From SqlDataReader

This .Net Visual Basic code snippet connects to SQL server and executes SQL statement and return SqlDataReader. To use this function simply provide database connection string and SQL statement. This function uses SqlClient name space to get data using SqlDataReader. Modify the exception handling section to your project requirements. To modify the function to use global database connection, remove the local SqlConnection and make it global, in this way only variable need to pass is SQL statement.

Public Function GetSqlDataReader(ByVal _ConnectionString As String, ByVal _SQL As String) As System.Data.SqlClient.SqlDataReader
    ' Set temporary variable to create data reader
    Dim _SqlDataReader As System.Data.SqlClient.SqlDataReader = Nothing

    ' set temporary variable for database connection
    Dim _SqlConnection As New System.Data.SqlClient.SqlConnection()

    ' assign database connection string
    _SqlConnection.ConnectionString = _ConnectionString

    ' Connect to database
    Try
        _SqlConnection.Open()
    Catch _Exception As Exception
        ' Error occurred while trying to connect to database
        ' send error message to console (change below line to customize error handling)
        Console.WriteLine(_Exception.Message)

        ' failed connection, lets return null
        Return Nothing
    End Try

    ' Database connection successful
    ' lets run the given SQL statement and get the data to SqlDataReader
    Try
        ' Pass the connection to a command object
        Dim _SqlCommand As New System.Data.SqlClient.SqlCommand(_SQL, _SqlConnection)

        ' get query results
        _SqlDataReader = _SqlCommand.ExecuteReader()

    Catch _Exception As Exception
        ' Error occurred while trying to execute reader
        ' send error message to console (change below line to customize error handling)
        Console.WriteLine(_Exception.Message)

        ' failed SQL execution, lets return null
        Return Nothing
    End Try

    ' SQL successfully executed, lets return the SqlDataReader
    Return _SqlDataReader
End Function


Here is a simple example showing how to use above function (GetSqlDataReader) to connect to SQL database and get SqlDataReader for given SQL statement.

' Lets call above function to create a new SqlDataReader
' using given database connection string and SQL statement
    ' Pass database connection string to function
    ' Pass SQL statement to create SqlDataReader
Dim _SqlDataReader As System.Data.SqlClient.SqlDataReader = GetSqlDataReader("Server=SERVERADDRESS;Database=DATABASENAME;Uid=USERID;Pwd=PASSWORD;", "SELECT NAME, PRICE, COST FROM INVENTORY")

' Check we have data in SqlDataReader
If _SqlDataReader IsNot Nothing AndAlso _SqlDataReader.HasRows Then
    ' We have data in SqlDataReader, lets loop through and display the data
    Do While _SqlDataReader.Read()
        Console.WriteLine(_SqlDataReader("NAME").ToString())
    Loop

    ' Close SqlDataReader
    _SqlDataReader.Close()
    _SqlDataReader.Dispose()
End If


VB Keywords Used:

  • SqlDataReader
  • SqlConnection
  • ConnectionString
  • SqlCommand
  • ExecuteReader
  • Exception
  • Do While Loop
  • Close
  • Dispose
  • IsNot
  • Nothing
  • AndAlso
  • If End If

Code Snippet Information:

  • Applies To: .Net, VB, Visual Basic, CLI, SqlDataReader, SQL, SQL Server, SQL Client, Connection String, Database Connection, SQL Data Reader
  • Programming Language : VB (Visual Basic)

External Resources:

Leave a comment