Connection - (ADO.NET and SQL Server)
In ADO.NET, a Connection object is used to establish a connection to a data source, such as a SQL Server database. The Connection object provides methods and properties for opening and closing connections, and for executing SQL commands against the data source.
Syntax
To use a Connection object in ADO.NET, you need to create an instance of the SqlConnection class, passing in the connection string as a parameter:
string connectionString = "<connection string>";
SqlConnection connection = new SqlConnection(connectionString);
You can then open the connection using the Open()
method:
connection.Open();
To close the connection, you can call the Close()
method:
connection.Close();
Example
Here's an example of using a Connection object to execute a simple SQL query against a SQL Server database:
string connectionString = "<connection string>";
string sql = "SELECT * FROM Customers";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("{0} - {1}", reader["CustomerID"], reader["CompanyName"]);
}
reader.Close();
connection.Close();
In this example, we create a SqlConnection object using a connection string, and then create a SqlCommand object with a simple SQL query that selects all rows from the Customers table. We then open the connection, execute the query using the ExecuteReader()
method, and loop through the result set using a SqlDataReader. Finally, we close the reader and the connection.
Output
When you run this code, it retrieves the list of customers from the SQL Server database and prints the CustomerID and CompanyName values to the console.
Explanation
The Connection object is used in ADO.NET to manage connections to data sources, such as SQL Server databases. The SqlConnection class represents a connection to a SQL Server database, and provides methods and properties for opening and closing connections, and for executing SQL commands against the data source.
Use
The Connection object is used whenever you need to connect to a SQL Server database to perform operations such as querying, inserting, updating, and deleting data. It provides a way to establish and manage a connection to the database, and can be used in combination with other ADO.NET objects such as Command and DataReader.
Important Points
- The Connection object is used to establish a connection to a data source, such as a SQL Server database.
- The SqlConnection class is used to create a connection to a SQL Server database.
- The
Open()
method is used to open the connection, and theClose()
method is used to close it. - The Connection object can be used in combination with other ADO.NET objects such as Command and DataReader to perform operations on the data source.
Summary
In this page, we discussed the basics of using the Connection object in ADO.NET to connect to a SQL Server database. We covered the syntax, example, output, explanation, use, and important points of the Connection object. The Connection object is a key component of ADO.NET and is used whenever you need to connect to a data source to perform operations on the data.