using System;
using System.Data.SqlClient;
class Program
{
static void Main()
{
string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;";
string sqlQuery = "SELECT * FROM Customers";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(sqlQuery, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine($"{reader["CustomerID"]}, {reader["CompanyName"]}, {reader["ContactName"]}");
}
reader.Close();
}
}
}
ADO.NET is a powerful framework for data access in the Microsoft .NET platform. It stands for ActiveX Data Objects.NET, and it's a set of classes that allows developers to interact with data sources like databases or XML files. ADO.NET provides consistent access to data sources such as SQL Server, Oracle, MySQL, and others, using a variety of programming models.
Overview of ADO.NET
ADO.NET is designed to be efficient, flexible, and easy to use. It consists of several key components:
-
Data Providers: ADO.NET includes data providers for various database systems, each tailored to the specific features and capabilities of the target database. For example, System.Data.SqlClient
is the data provider for SQL Server, while Oracle.DataAccess.Client
is used for Oracle databases.
-
Connections: A connection object represents a connection to a data source. It manages the connection state, including opening and closing connections to the database. ADO.NET supports connection pooling to improve performance by reusing connections.
-
Commands: A command object represents a SQL statement or stored procedure to be executed against the database. ADO.NET supports two main types of commands: SqlCommand
for executing SQL queries and OracleCommand
for Oracle databases.
-
DataReaders: A DataReader provides a forward-only, read-only stream of data from a data source. It is ideal for scenarios where you need to quickly read and process large volumes of data, as it minimizes memory usage and provides fast access to data.
-
DataAdapters: A DataAdapter serves as a bridge between a DataSet and a data source. It populates a DataSet with data from the database and reconciles changes made to the DataSet back to the database.
-
DataSets and DataTables: ADO.NET includes in-memory data storage objects like DataSets and DataTables. A DataSet represents an in-memory cache of data retrieved from the database, while a DataTable represents a single table within the DataSet.
Benefits of ADO.NET
-
Performance: ADO.NET is optimized for performance, with features like connection pooling, efficient data access, and minimal memory overhead.
-
Disconnected Data Access: ADO.NET supports disconnected data access, allowing applications to retrieve data from the database, work with it locally, and submit changes back to the database when needed.
-
Data Binding: ADO.NET integrates seamlessly with data binding in .NET applications, making it easy to display data from a database in user interfaces such as Windows Forms or ASP.NET web applications.
-
Security: ADO.NET includes features for securing database connections and executing parameterized queries to prevent SQL injection attacks.