Creation of connections to a database in ADO.NET

The first, that is necessary for us at job with a database is a creation of connection to her. Thus it is necessary to specify where she is, by what machine in a network, avtorizirovat`sja, to specify a concrete database in SUBD. Thus in zavisimostii from SUBD, requirements to definition of these parameters vary. Below we shall try to consider examples of connection to "big", designed on a plenty of active sessions (simultaneous connections) Microsoft SQL Server and "small" and unpretentious base Access.


For job with the data you should use the following spaces of names

using System. Data;

using System. Data. SqlClient; // If it is used SQL Server

using System. Data. OleDb; // If it is used OLEDB



For connection to a database in object Connection ADO.NET responds. We shall consider two updatings - univesal`nuju OleDbConnection and designed specially for job with MS SQL Serer - SqlConnection. « njTo, that we do not consider{examine} the others, their absence, simply a principle of job with them similar, and, to tell the truth, does not mean at all with others I yet did not work. It was not necessary.


New object Connection is created standardly:

SqlConnection sqlConn = new SqlConnection ();

// We open connection

sqlConn. Open ();

/*rabotaem With a database also is forgotten to close connection, when it any more neobkhodimo*/

sqlConn. Close ()



There is an overloaded designer where it is possible to specify a line of connection at once:

string strConnection = » user id=sa; data source = (local); persist security info=True; initial " +

« catalog=h_asash; password=myPass » ";

SqlConnection sqlConn = new SqlConnection (strConnection);



The line of connection also can be specified, isrol`zuja property ConnectionString:

sqlConn. ConnectionString = « user id=sa; data source = (local); persist security info=True; initial » + « catalog=h_asash; password=myPass »;



Now it is necessary for you to know the connections following about parameters:

user id         Login name SUBD

password         The password

data source         A way to SUBD, for example db5.daoto.net, SASH, (local), through a point it is possible to specify port: db5.daoto.net, 1433

initial catalog         A database

persist security info         The task of politics of safety


So, perfectly to be connected to SQL Server you already are able. Now we shall consider connection to MS Access:

string sqlConn = « Provider=Microsoft. Jet. OLEDB.4.0; Data Source=E:AccessBasesbase1.mdb »;

OleDbConnection oleDbConn = new OleDbConnection (strConn);



Or, if there is a password on database Access:

string sqlConn = « Provider = Microsoft. Jet. OLEDB.4.0; Data Source = E:AccessBasesbase1.mdb; JetOLEDB:Database Password=passwd »;

OleDbConnection oleDbConn = new OleDbConnection ();

oleDbConn. ConnectionString=strConn;



Provider         Supplier OLEDB. For MS Access: Microsoft. Jet. OLEDB.4.0

Data Source         A way to a database

Jet OLEDB:Database Password         The password


Well, perhaps and everything, do not overlook to close unnecessary connections:

conn. Close ();