What is connection string C#?

sql server connection string,What is connection string C#?
Share it:
sql server connection string


sql server connection string

The Connection String is a series of name/value pairs separated by semicolon that indicate the settings for connecting to a database. Such settings include, the situation of the database, authentication, security, and therefore the name of the database to attach to. Connection strings for various datasources aren't precisely the same. For example, OLE DB and ODBC requires an OLE DB and ODBC driver to be laid out in their connection strings. The Connection classes of the info providers provides a ConnectionString property that you simply 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 datasource:

sql connection string

"Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind; Integrated Security=SSPI"

The following table are the essential connection string parameters that you simply can use when building your connection string. Note that not all parameters are an equivalent for each data source.

ParameterDescription
AttachDBFileName / Initial File NameUsed 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.
Connect Timeout / Connection TimeoutThe 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. ;
Data Source / Server / Address / Addr / Network AddressThe server name or network address of the database product to connect to. Use localhost for the current computer.
Initial Catalog / DatabaseThe database the the connection will initially use.
Integrated Security / Trusted_ConnectionDefaults to false. When set to true or SSPI, the .NET provider attempts to connect to the data source using Windows integrated security.
Persist Security InfoWhen set to false (the default), security-sensitive information such 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 IDThe database 


sql connection string Parameters


The following example shows you how to add connection string to a connection.

SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = @"Data Source=localhost\SQLEXPRESS;" +
    "Initial Catalog=Northwind;Integrated Security=SSPI";

Alternatively, you can use an overloaded constructor of the Connection class that accepts one argument which is the connection string.

SqlConnection myConnection = new SqlConnection(@"Data Source=localhost\SQLEXPRESS;" +
    "Initial Catalog=Northwind;Integrated Security=SSPI");

The connection string shown above is used for connecting to an SQL Express datasource. As specefied by the Data Source parameter, “localhost\SQLEXPRESS“, we are connecting to a server named SQLEXPRESS that lies within the current computer(localhost). Alternatively, you can use a dot instead of writing localhost so this is equivalent: “.\SQLEXPRESS”

The Initial Catalog parameter indicates the first database to use. You can change the database during runtime by calling the Connection . Change Database method or executing the SQL command “USE DATABASE ” where is the name of the database that will replace the current database. Later lessons will show how to execute not query SQL commands such as CREATE, UPDATE, and DELETE.

Integrated Security was set to SSPI (Security Support Provider Interface) signifying the we will be using the credentials of the current windows user. You can change this to a selected database user by replacing Integrated Security with the User Id and Password parameters.

@"Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;"
+ "User Id=database_user;Password=the_password"

.NET also provides the ConnectionStringBuilder class for every of the info providers. For example, we can use the SqlConnectionStringBuilder class to create SQL Server connection strings.
Share it:

adonet

sql

Post A Comment:

0 comments: