C# > Data Access > Database Access > SqlConnection, SqlCommand, SqlDataReader
Basic Database Query with SqlConnection, SqlCommand, and SqlDataReader
This snippet demonstrates how to connect to a SQL Server database, execute a query, and read the results using SqlConnection, SqlCommand, and SqlDataReader in C#.
Code Snippet
This code connects to a SQL Server database, executes a SELECT query, and prints the Id and Name from each row in 'YourTable'. The using
statements ensure that resources are properly disposed of, even if an exception occurs. Remember to replace the placeholder values in the connection string with your actual database credentials.
using System;
using System.Data.SqlClient;
public class DatabaseAccess
{
public static void GetData()
{
string connectionString = "Server=your_server;Database=your_database;User Id=your_user_id;Password=your_password;";
string queryString = "SELECT Id, Name FROM YourTable;";
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1]));
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Concepts Behind the Snippet
This snippet utilizes three core classes from the System.Data.SqlClient
namespace:
The using
statement is crucial for proper resource management. It ensures that the SqlConnection
, SqlCommand
, and SqlDataReader
objects are disposed of when they are no longer needed, releasing database connections and other resources. Failing to do so can lead to connection leaks and performance issues.
Real-Life Use Case
Imagine a scenario where you need to display a list of products from a database on a web page. This snippet provides the fundamental logic to retrieve product data (Id, Name, Description, etc.) from your database and then prepare that data to be presented in a user-friendly format within your web application. Another use case might be generating reports, where you need to extract specific data from a database and format it for analysis.
Best Practices
try-catch
blocks to gracefully handle exceptions and prevent application crashes. Log errors for debugging and troubleshooting.using
statements to ensure that database connections and other resources are properly disposed of.
Interview Tip
Be prepared to explain the purpose of each class (SqlConnection, SqlCommand, SqlDataReader), their role in database interaction, and the importance of proper resource disposal. Also, understand the concept of connection pooling and its benefits. Be able to discuss security considerations like SQL injection and how to prevent it.
When to Use Them
Use SqlConnection
, SqlCommand
, and SqlDataReader
when you need fine-grained control over database interactions and performance is critical. These classes are particularly useful for simple queries and data retrieval scenarios. For more complex scenarios or when you prefer an object-relational mapping (ORM) approach, consider using Entity Framework or Dapper.
Memory Footprint
SqlDataReader
offers a relatively low memory footprint because it retrieves data in a forward-only, read-only manner. It doesn't load the entire result set into memory at once, making it suitable for handling large datasets. However, the SqlConnection
object itself does consume resources while open, so it's essential to close the connection as soon as you're finished with it.
Alternatives
Alternatives to using SqlConnection, SqlCommand, and SqlDataReader directly include:
Pros
Cons
FAQ
-
How do I handle SQL injection vulnerabilities?
Use parameterized queries or stored procedures to prevent SQL injection. Never directly concatenate user input into SQL queries. -
What is the purpose of the 'using' statement?
The 'using' statement ensures that objects that implement theIDisposable
interface (like SqlConnection, SqlCommand, and SqlDataReader) are properly disposed of, releasing resources and preventing memory leaks. -
How do I handle connection strings securely?
Store connection strings in a configuration file (e.g., appsettings.json) or use Azure Key Vault for secure storage. Encrypt the connection string if necessary.