Web. config connectionStrings database connection string Interpretation (reprint)

Source: Internet
Author: User
Tags connectionstrings

Let's take a look at the default connection SQL Server database configuration
<connectionStrings>
<add name= "LocalSqlServer" connectionstring= "Data source=.\sqlexpress;integrated Security=sspi; attachdbfilename=| Datadirectory|aspnetdb.mdf; User instance=true "providername=" System.Data.SqlClient "/>
</connectionStrings>

You need to use ConnectionString when instantiating SqlConnectionStringBuilder. such as: sqlconnectionstringbuild builder = new Sqlconnectionstringbuild (connectionString);


First, Data Source
The DataSource property of the SqlConnectionStringBuilder, corresponding to the data source in connectionstring, "data source" can be replaced by the following string: "Server", " Address "," Addr "and" Network address ".
Data source=.\sqlexpress can also be written as such data source= (local) \SQLExpress.

Second, Integrated Security
The IntegratedSecurity attribute of the SqlConnectionStringBuilder, corresponding to the I ntegrated security in connectionString, "Integrated security" Can be written as "Trusted_Connection".
True to use the current Windows account credentials for authentication, false when the user ID and password need to be specified in the connection. The recognized values are true, False, Yes, No, and SSPI that is equivalent to true.
If you don't have some, you have to write a "UID" such as uid=sa;pwd=123, you can also use "User ID", "pwd" can also be changed to "PassWord".

The Sspi:microsoft Security Support Provider Interface (SSPI) is a well-defined public API that is used to obtain integrated security services such as authentication, information integrity, information privacy, and security services for all distributed application protocols.
The Application Protocol designer can take advantage of this interface to obtain different security services without modifying the protocol itself.

Third, AttachDbFileName
The AttachDbFileName attribute of the SqlConnectionStringBuilder, corresponding to the AttachDbFileName in ConnectionString, "AttachDbFileName" can be written as " Extended Properties "," Initial file name ".
The AttachDbFileName property specifies the location of the database file that is dynamically attached to the server when the connection is opened.
This property can accept the full path and relative path of the database (for example, using | datadirectory| syntax), this path will be replaced by the application's App_Data directory at run time.

Iv. User Instance
The Userinstance property of the SqlConnectionStringBuilder that corresponds to the User Instance in ConnectionString, which indicates whether the connection is redirected from the default SQL Server instance to the The instance that is run under and started at run time.
Userinstance=true, sqlserverexpress to attach the database to the new instance, set up a new process to run under the identity of the user who opened the connection.
In an ASP. NET application, this user is the local ASPNET account or the default NetworkService, which relies on the operating system.
In order to securely attach a database file provided by a non-system administrator account (such as an ASP. NET account), it is necessary to establish a separate instance of SQL Server user.

V. Initial Catalog is equivalent to Database.

Vi. providerName Specifies the value "System.Data.OracleClient", which specifies that ASP. NET. NET System.Data.OracleClient provider should be used when using this connection string for connection.


Ps:
What is DataDirectory?

ASP. NET 2.0 has a special directory app_data, usually the SQL Server 2005 Express data file is placed in this directory, the corresponding database connection string is:
Connectionstring= "..... data source=.\sqlexpress;integrated Security=sspi; attachdbfilename=| Datadirectory|data.mdf; User Instance=true "
Here's a DataDirectory macro, what does it mean?

DataDirectory is a replacement string that represents the database path. Without hard-coding the full path, DataDirectory simplifies the sharing of projects and the deployment of applications. For example, you do not need to use the following connection string:
"Data source= c:\program Files\myapp\app_data\mydb.mdf"
By using | Datadirectory| (included in the vertical bar shown below), you can have the following connection string:
"Data Source = | Datadirectory|\mydb.mdf ".

It is not only used in SQL Server 2005 Express, but also in other file databases, such as the connection string for Sqllite database files:

<add name= "Defaultdb"
Connectionstring= "Driverclass=nhibernate.driver.sqlite20driver;dialect=nhibernate.dialect.sqlitedialect;data source=| Datadirectory|\data.db3 "/>

---------------------------------------attach some examples of connection statements---------------------------------------

<--General Example 1-->
<configuration>
<connectionStrings>
<add name= "Sales" providername= "System.Data.SqlClient" connectionstring= "Server=myserver;database=products;uid =salesuser;pwd=sellmoreproducts "/>

<add name= "NorthWind" providername= "System.Data.SqlClient" connectionstring= "server=.; database=northwind;integrated Security=sspi "/>

</connectionStrings>
</configuration>

<--General Example 2-->
<configuration>
<connectionStrings>
<add name= "NorthWind" connectionstring= "provider=sqloledb;integrated security=sspi;initial Catalog=da;Data Source=bar "/>
</configuration>

----------------------------------------------------------------------------------------------------------

<connectionStrings>
<add name= "LocalSqlServer" connectionstring= "Data source=.\sqlexpress;initial catalog=northwind;integrated Security=sspi "providername=" System.Data.SqlClient "/>
</connectionStrings>
<--can also be written as-
<connectionStrings>
<add name= "LocalSqlServer" connectionstring= "server=.\sqlexpress;database=northwind;integrated Security=Yes" Providername= "System.Data.SqlClient"/>
</connectionStrings>

----------------------------------------------------------------------------------------------------------

<configuration>
<connectionStrings>
<add name= "Db2005_2"
Providername= "System.Data.SqlClient"
connectionstring= "Data source=.;i Nitial Catalog=northwind; User Id=dbtester; Password=zhi; Trusted_connection=false; Connect timeout=30; Min Pool size=16; Max Pool size=100 "/>

<add name= "Db2005_1"
Providername= "System.Data.SqlClient"
Connectionstring= "server=.;D Atabase=northwind; User Id=dbtester; Password=zhi; Trusted_connection=false; Connect timeout=30; Min Pool size=16; Max Pool size=100 "/>

<add name= "Northword2000"
Providername= "System.Data.SqlClient"
connectionstring= "Initial Catalog=northwind; User Id=dbtester; Password=zhi; Persist Security info=false;data source= (local); Connect timeout=30; Min Pool size=16; Max Pool size=100; " />

<add name= "Sqlexp"
Providername= "System.Data.SqlClient"
connectionstring= "Data source=.\sqlexpress; attachdbfilename=| Datadirectory|\northwnd1.mdf;integrated security=true; Connect timeout=30; User instance=true "/>

<add name= "Oracle"
connectionstring= "Data source=test; User Id=sa; Password=sa; "
Providername= "System.Data.OracleClient"/>

<add name= "Oleconn"
Providername= "System.Data.OleDb"
connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=| Datadirectory|northwind.mdb "/>

<add name= "MYSQL"
Providername= "MySql.Data.MySqlClient"
Connectionstring= "server=172.29.131.27; Port=3311;database=comctl; Persist Security Info=false; User Id=root; password=123456; Allow Zero datetime=true; "/>
</connectionStrings>
<system.data>
<DbProviderFactories>
<add name= "MySQL Data Provider"
Invariant= "MySql.Data.MySqlClient"
Description= ". Net Framework Data Provider for MySQL"
Type= "MySql.Data.MySqlClient.MySqlClientFactory, Mysql.data, version=5.2.5.0, Culture=neutral, publickeytoken= C5687fc88969c44d "/>
</DbProviderFactories>
</system.data>
</configuration>

====================================== can be saved as (hereafter transferred from elsewhere). CS ======================================

Using System.Data.SqlClient;
Using System.Configuration;
public class Class1
{
Public Class1 ()
{
Persist Security Info If the connection password is no longer required after the database connection is successful, it is recommended that false
String sql2000 = "Initial catalog=northwind; User Id=dbtester; Password=zhi; Persist Security info=false;data source= (local); Connect timeout=30; Min Pool size=16; Max Pool size=100; ";
String sql2000 = "Initial catalog=northwind; User Id=sa; password=5; Persist Security info=false;data source=. ";
String sql2000 = "Initial catalog=northwind; User Id=sa; password=5; Persist Security info=false;data Source=newtime ";
String Source = "server= (local); integrated security=sspi;database=northwind";
String expresssource = @ "Data source=.\sqlexpress; attachdbfilename=| Datadirectory|\northwnd.mdf;integrated security=true; Connect timeout=30; User Instance=true ";
Integrated Security uses Integrated Windows authentication, integraged Security=sspi;
Integrated Security=sspi This indicates that the current Windows system user is logged on to the SQL Server server and an error occurs if the SQL Server server does not support this way of logging on
Indicates your connection security authentication method, can be replaced by Trusted_connection=yes
Integrated Security is True. User instances are only used with integrated security, and SQL Server users with user names and passwords do not work.
String source3 = @ "Data source=190.190.200.100,1433; Network library=dbmssocn;initial catalog= pubs; Userid=sa; PASSWORD=ASDASD; ";
(DBMSSOCN=TCP/IP instead of Named Pipes, at the end of the that Data Source is the port to use (1433 is the default))
Standard Security:
String source4 = "Data source=aron1;initial catalog= pubs; Userid=sa; PASSWORD=ASDASD; ";
String source5 = "server=aron1;database=pubs; Userid=sa; PASSWORD=ASDASD; Trusted_connection=false ";
Trusted_Connection ' false ' when false, a user ID and password will be specified in the connection. When True, the current Windows account credentials are used for authentication.
The recognized values are true, false, yes, no, and SSPI, which is equivalent to true (highly recommended). So be sure to set trusted_connection= false, in case someone else "login", "inject statement" and so on
String source6 = "Data source=aron1;initial catalog=pubs;integrated security=sspi;";
String source7 = "server=aron1;database=pubs; Trusted_connection=true; ";
(Use ServerName\InstanceName as Data Source for use of an Specifik SQL Server instance, only SQLServer2000)
Integrated security or Trusted_Connection ' false ' when false,
The user ID and password will be specified in the connection. When True, the current Windows account credentials are used for authentication
Vs2003:string connstr=system.configuration.configurationsettings.appsettings["SQLCONNECTIONSTRING"];2003.
Vs2005:string connstr=system.configuration.configurationmanager.connectionstrings["SQLCONNECTIONSTRING"]. ToString (); 2005-8
System.Configuration.ConnectionStringSettings i = system.configuration.configurationmanager.connectionstrings[" Northword2000 "];
}
}

Web. config connectionStrings database connection string Interpretation (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.