A Closer Look at ADO.NET: The Connection Object
(Page 1 of 5 )
The first thing we need to do with a database application is establish a connection to the database. ADO.NET handles this by using connection classes. In this article, Michael Youssef shows you how to start using connection classes, with examples.
When developing database applications using .NET, the very first thing that we need is a connection to the database. ADO.NET provides us with connection classes like the SqlConnection class and OleDbConnection class. The SqlConnection class is part of the SQL Server .NET Data Provider. This data provider has been designed for performance optimization with SQL Server 7.0 and later.
The OleDbConnection is part of the OLEDB .NET Data Provider, which is used to access a data source that has an OLEDB Provider. In this article (and actually most of my articles on ADO.NET) I use the SQL Server .NET Data Provider. So let's talk a little about the SqlConnection class before we write code.
The SqlConnection class is part of the namespace System.Data.SqlClient (the namespace that forms the SQL Server .NET Data Provider). The Connection classes (SqlConnection, OleDbConnection and the other available classes like OracleConnection) implement the IDbConnection Interface, which has properties such as ConnectionString, ConnectionTimeout, Database, State. It also features methods such as Open(), Close(), BeginTransaction(), ChangeDatabase() and CreateCommand(). We will discuss all of these properties and methods.
You can be sure that there are common interfaces for all of the connection classes (through the IDbConnection), but of course each connection class can implement other interfaces that are needed for its operations. That means there are more methods than the methods of the IDbConnection Interface. To open a connection with the database we have to instantiate a SqlConnection object and set some properties (such as the ConnectionString), then call the Open() method to establish the connection with the database. The ConnectionString property takes a key/value pair string value that represents information about the server, database, user information and other information that is needed to connect to the specific server. After you set the ConnectionString property, you can open the connection. Let's look at an example.
Next: The Example >>
More Database Code Articles
More By Michael Youssef