C# Code Snippet - Get Data From SqlDataReader using open connection
(C-Sharp) C# code snippet connects to SQL server and executes SQL statement and return SqlDataReader. GetSqlDataReader returns SqlDataReader using open database connection and SQL statement.
Bookmark:
C# Code Snippet - Get Data From SqlDataReader using open connection
This .Net C# code snippet connect connects to SQL server and executes SQL statement and return SqlDataReader. To use this function simply provide open database connection and SQL statement. This function uses SqlClient name space to get data using SqlDataReader. Modify the exception handling section for your project requirements.
public System.Data.SqlClient.SqlDataReader GetSqlDataReader( ref System.Data.SqlClient.SqlConnection _SqlConnection, string _SQL) { // Set temporary variable to create data reader System.Data.SqlClient.SqlDataReader _SqlDataReader = null; // lets run the given SQL statement and create SqlDataReader try { // Pass the connection to a command object System.Data.SqlClient.SqlCommand _SqlCommand = new System.Data.SqlClient.SqlCommand(_SQL, _SqlConnection); // get query results _SqlDataReader = _SqlCommand.ExecuteReader(); } catch (Exception _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 null; } // SQL successfully executed, lets return the SqlDataReader return _SqlDataReader; }
Here is a simple example showing how to use above function (GetSqlDataReader) to connect to SQL database and get SqlDataReader for given SQL statement.
// set temporary variable for database connection System.Data.SqlClient.SqlConnection _SqlConnection = new System.Data.SqlClient.SqlConnection(); // assign database connection string _SqlConnection.ConnectionString = "Server=SERVERADDRESS;Database=DATABASENAME;Uid=USERID;Pwd=PASSWORD;"; // Connect to database try { _SqlConnection.Open(); } catch (Exception _Exception) { // Error occurred while trying to connect to database Console.WriteLine(_Exception.Message); } // Check for valid open database connection before query database if (_SqlConnection != null && _SqlConnection.State == ConnectionState.Open) { // Lets call above function to create a new SqlDataReader // using open database connection and SQL statement System.Data.SqlClient.SqlDataReader _SqlDataReader = GetSqlDataReader( // Pass open database connection to function ref _SqlConnection, // Pass SQL statement to create SqlDataReader "SELECT NAME, PRICE, COST FROM INVENTORY"); // Check we have data if (_SqlDataReader != null && _SqlDataReader.HasRows) { Console.WriteLine(_SqlDataReader["NAME"].ToString()); } // close sql reader if (_SqlDataReader != null) { _SqlDataReader.Close(); _SqlDataReader.Dispose(); } // close database connection _SqlConnection.Close(); }
C# Keywords Used:
- SqlDataReader
- SqlConnection
- ConnectionString
- SqlCommand
- ExecuteReader
- Exception
Code Snippet Information:
- Applies To: .Net, C#, CLI, SQL, SQL Server, SQL Client, Connection String, Database Connection, SQL Data Reader
- Programming Language : C# (C-Sharp)
External Resources: