Sunteți pe pagina 1din 3

Connection Strings

connection string is a series of name/value pairs seperated by semicolon that indicate the settings
for connecting to a database. Such settings include, the location of the database, authentication,
security, and the name of the database to connect to. Connection strings for different data sources are
not exactly the same. For example, OLE DB and ODBC requires an OLE DB and ODBC driver to be
specified in their connection strings. The Connection classes of the data providers provides
a ConnectionString property that you can use to specify the connection string for connecting to a
database. Here is an example of a connection string for connecting to a SQL Server Expres data
source:
Data Source=.\SQLEXPRESS;Initial Catalog=University; Integrated Security=SSPI;

Figure 1 - A connection string example


For a list of connection strings for different providers, you can go to the following site:
http://connectionstrings.com
The following table are the basic connection string parameters that you can use when building your
connection string. Note that not all parameters are the same for every data source.
Parameter
AttachDBFileName / Initial
File Name

Connect Timeout /
Connection Timeout
Data Source / Server /
Address / Addr / Network
Address
Initial Catalog / Database
Integrated Security /
Trusted_Connection

Description
Used only if you want to connect to an attachable database file (for
example, an .mdf file that isn't registered with the database
system). Full version of SQL Server doesn't support this.
The length of time (in seconds) to wait for a connection to the server
before terminating the attempt and generating an error. Defaults to
15 seconds, and 0 seconds represents an infinite wait. ;
The server name or network address of the database product to
connect to. Uselocalhost for the current computer.
The database the the connection will initially use.
Defaults to false. When set to true or SSPI, the .NET provider
attempts to connect to the data source using Windows integrated
security.
When set to false (the default), security-sensitive information such

Persist Security Info

as the password is removed from the ConnectionString property as


soon as the connection is opened. Thus, you can't retrieve this
information in your code.

User ID

The database account user ID.

Password/Pwd

The password corresponding to the User ID.

Figure 2 - Connection String Parameters

The example that we will show here are connection strings for connecting to an SQL Server data
source. You can proceed to the website using the link above if you are finding the connection strings
for other data sources such as Access.
When building connection strings, you need to specify the data source or the server to be used. This
can be done using the Data Source or the Server parameters. The connection string shown below is
used for connecting to an SQL Express data source.
Data Source=.\SQLEXPRESS;Initial Catalog=University;Integrated Security=SSPI;
As specified by the Data Source parameter, ".\SQLEXPRESS", we are connecting to a server instance
named SQLEXPRESS that lies within the current computer(localhost). Alternatively, you can use the
word localhostinstead so this is equivalent: "localhost\SQLEXPRESS". You can also specify the server or
the computer name as the data source followed by the server instance. For example, suppose that the
server we are connecting to is named MyServer and we want to connect to the SQLEXPRESS server
instance, then the following connection string can be used.
Data Source=MyServer\SQLEXPRESS;Initial Catalog=University; Integrated Security=SSPI;
You can also use the Server parameter instead of the Data Source which does the same thing.
"Server=.\SQLEXPRESS;Initial Catalog=University; Integrated Security=SSPI"
We also need to specify the initial database to be used. The Initial Catalog or Database parameter
indicates the initial database to use. You can change the database during runtime by calling
theDbConnection.ChangeDatabase method or executing the SQL command "USE DATABASE
<Database Name>"where <Database Name> is the name of the database that will replace the
current database. Later lessons will show how to execute non-query SQL commands such as CREATE,
UPDATE, and DELETE. Alternatively, you can use the Database parameter instead.
Data Source=.\SQLEXPRESS;Database=University;Integrated Security=SSPI;
The connection string also requires authentication and security information. Integrated Security was
set to SSPI (Security Support Provider Interface) signifying the we will be using the credentials of the
current windows user.
Data Source=.\SQLEXPRESS;Initial Catalog=University; Integrated Security=SSPI;
Alternatively, you can use the Trusted_Connection parameter instead and set it to True.
Data Source=.\SQLEXPRESS;Initial Catalog=University; Trusted_Connection=True;
You can change this to a specific database user by replacing Integrated Security with the User Id and
Password parameters.
Data Source=.\SQLEXPRESS;Initial Catalog=University;
User Id=database_user;Password=the_password;
When using Sql Server Express, we can also attach a database file on a local SQL server instance. We
can use the AttachDbFilename to do just that.

Data Source=.\SQLEXPRESS;AttachDbFileName=C:\Data\University.mdf;
Database=University; Trusted_Connection=True;
We specified the path of the database file that we will use. If a Data Directory is defined, then you can
use the |DataDirectory| instead.
Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|University.mdf;
Database=University; Trusted_Connection=True;

ConnectionStringBuilder
.NET also provides the ConnectionStringBuilder class for each of the data providers.
ConnectionStringBuilder classes inherit from the DbConnectionStringBuilder base class. The following
shows the different flavors of the ConnectionStringBuilder class.
Provider

ConnectionStringBuilder Class

SQL Server

SqlConnectionStringBuilder

OLE DB

OleDbConnectionStringBuilder

ODBC

OdbcConnectionStringBuilder

Figure 3 - ConnectionStringBuilder Classes


We will use SQL Server provider as our example. We can use the SqlConnectionStringBuilder class to
create SQL Server connection strings.
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = @".\SQLEXPRESS";
builder.IntegratedSecurity = true;
builder.InitialCatalog = "Northwind";
ConnectionStringBuilder contains properties which corresponds to the different parameters of a
connection string. Each flavor of the ConnectionStringBuilder has different sets of properties because
connection strings for different data sources are different. You can then access the connection string
built by theConnectionStringBuilder using the ConnectionString property.
string connectionString = builder.ConnectionString;
There are more advanced parameters that you can use when building connection strings. Only the
basic ones were introduced here to give you an essential knowledge for building connection strings.

S-ar putea să vă placă și