Original: Web. config connectionStrings database connection string Interpretation (reprint)
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=NewSqlconnectionstringbuild (connectionString); the datasource attribute of Data Sourcesqlconnectionstringbuilder, corresponding to Data source in connectionstring, "data source" can be replaced by the following strings: "Server", "Address", "Addr" and "Network address". Data Source=.\sqlexpress can also be written like this data source=(local) \SQLExpress. Second, the integratedsecurity attribute of Integrated Securitysqlconnectionstringbuilder, 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 the UID.=sa;pwd=123settings such as "UID" 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, the AttachDbFileName attribute of Attachdbfilenamesqlconnectionstringbuilder, corresponding to 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 is replaced by the App_Data directory of the application at run time. The user Instancesqlconnectionstringbuilder's Userinstance property corresponds to the user Instance in ConnectionString, which indicates whether to connect from the default SQL Serve The R instance is redirected to an instance that runs under the caller account and starts at run time. Userinstance=true, sqlserverexpress to attach the database to the new instance, establish a new process, and 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. What is Ps:datadirectory??ASP2.0 has a special directory app_data, usually SQL Server2005The 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 have the following connection string:"Data Source = | Datadirectory|\mydb.mdf". More than just SQL Server2005Express, you can also use it in other file databases, such as the connection string for the Sqllite database file:<add name="Defaultdb"connectionString="Driverclass=nhibernate.driver.sqlite20driver;dialect=nhibernate.dialect.sqlitedialect;data Source=| DATADIRECTORY|\DATA.DB3"/>---------------------------------------Attach Some examples of connection statements---------------------------------------<--Common 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 (the following is transferred from elsewhere). CS ======================================usingSystem.Data.SqlClient;usingSystem.Configuration; Public classclass1{ PublicClass1 () {//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 "; stringSource ="server= (local); Integrated Security=sspi;database=northwind"; stringExpresssource =@"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: stringSource4 ="Data source=aron1;initial catalog= pubs; Userid=sa; PASSWORD=ASDASD;"; stringSource5 ="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 stringSource6 ="Data source=aron1;initial catalog=pubs;integrated security=sspi;"; stringSource7 ="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-8System.Configuration.ConnectionStringSettings i = system.configuration.configurationmanager.connectionstrings["Northword2000"]; }}
Web. config connectionStrings database connection string interpretation