Connecting and Accessing Data through ADO.NET


Introduction

ADO.NET (ActiveX Data Objects .NET) is a data access technology provided by Microsoft that allows developers to connect to and access data from various databases in a consistent and efficient manner. In this article, we will explore how to connect and access data through ADO.NET, covering topics such as establishing database connections, executing SQL queries, retrieving data, and performing data manipulation operations.

1. Establishing Database Connections

The first step in accessing data through ADO.NET is establishing a connection to the database. ADO.NET provides the SqlConnection class, which represents a connection to a SQL Server database. To establish a database connection, we need to provide the connection string, which contains information such as the server name, database name, authentication details, etc.

```csharp
using System.Data.SqlClient;

string connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
SqlConnection connection = new SqlConnection(connectionString);

try
{
connection.Open();
Console.WriteLine("Connection established successfully!");
}
catch (SqlException ex)
{
Console.WriteLine("Failed to establish connection: " + ex.Message);
}
finally
{
connection.Close();
}
```

2. Executing SQL Queries

Once the connection is established, we can execute SQL queries to retrieve or manipulate data in the database. ADO.NET provides the SqlCommand class to execute SQL commands. We can specify the SQL query as a string and execute it using the ExecuteNonQuery, ExecuteScalar, or ExecuteReader methods.

```csharp
string sqlQuery = "SELECT * FROM Customers";
SqlCommand command = new SqlCommand(sqlQuery, connection);

try
{
SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
// Process retrieved data
}

reader.Close();
}
catch (SqlException ex)
{
Console.WriteLine("Failed to execute SQL query: " + ex.Message);
}
```

3. Retrieving Data

To retrieve data from a query result, we use the SqlDataReader class, which provides a forward-only, read-only stream of rows from the database. We can access individual columns by their index or name using the reader's GetXXX methods, where XXX represents the appropriate data type (e.g., GetString, GetInt32, etc.).

```csharp
while (reader.Read())
{
string name = reader.GetString(0);
int age = reader.GetInt32(1);
// Process retrieved data
}
```

4. Performing Data Manipulation Operations

ADO.NET also allows us to perform data manipulation operations like insert, update, and delete. For these operations, we utilize the ExecuteNonQuery method, which returns the number of rows affected by the command. We can construct and execute SQL statements using parameters to ensure secure and efficient data manipulation.

```csharp
string sqlInsert = "INSERT IGNORE INTO Customers (Name, Age) VALUES (@Name, @Age)";
SqlCommand insertCommand = new SqlCommand(sqlInsert, connection);
insertCommand.Parameters.AddWithValue("@Name", "John Doe");
insertCommand.Parameters.AddWithValue("@Age", 30);

try
{
int rowsAffected = insertCommand.ExecuteNonQuery();
Console.WriteLine(rowsAffected + " row(s) inserted successfully!");
}
catch (SqlException ex)
{
Console.WriteLine("Failed to execute SQL command: " + ex.Message);
}
```

Conclusion

ADO.NET provides a powerful and flexible framework for connecting and accessing data from databases. It allows developers to establish database connections, execute SQL queries, retrieve data, and perform data manipulation operations in a consistent and efficient manner. By understanding the concepts and techniques outlined in this article, developers can effectively leverage ADO.NET to connect and access data in their applications.