Connection Object in ADO.net:

May 11, 2011

The ADO.net Connection Object is used to establish a connection to a data source or a database. Using this connection, you can access and manipulate a database.
To establish connection we pass connection string to ADO.net Connection Object. Using this connection string string it establishes connection with the given data source. We can also make a connection to a database by passing a connection string to a Command or Recordset object.

Connection string Example:

Properties Explanation
initial catalog Sets or returns name of the database.
Data Source Sets SQL Server instance located on the local machine.
Connection Timeout Sets or returns the number of seconds to wait for a connection to open
User Id Sets or returns User name for the database
Password Sets password for the database
Min Pool Size The minimum number of connections allowed in the pool.
Max Pool Size The maximum number of connections allowed in the pool.
Incr Pool Size Controls the number of connections that are established when all the connections are used.
Decr Pool Size Controls the number of connections that are closed when an excessive amount of established connections are unused.

Different types of connection strings:

SQL Server: initial catalog=database_name; Data Source =database_server; Connection Timeout=30; User Id=user_name; Password=Password; Min Pool Size=20; Max Pool Size=200; Incr Pool Size=10; Decr Pool Size=5;
Excel: Provider=Microsoft.ACE.OLEDB.12.0;Data Source=file_name;Extended Properties="Excel 12.0 Xml;HDR=YES";
Access : Provider=Microsoft.Jet.OLEDB.4.0;Data Source= file_name;Jet OLEDB:Database Password=password;

Complete Code (SQL):

Using System.Data.Sqlclient; SqlConnection conn = new SqlConnection ("initial catalog=database_name; Data Source =database_server; Connection Timeout=30; User Id=user_name; Password=Password; Min Pool Size=20; Max Pool Size=200; Incr Pool Size=10; Decr Pool Size=5;"); Conn.Open(); //Execute your code Conn.Close();