Some summary of the Web. config connection string

Source: Internet
Author: User
Tags connectionstrings

Read the catalogue :

    • DS01: Two ways to type a database connection string
    • DS02: The contents of the database connection string

DS01: Two ways to type a database connection string

1. Two types of connection strings:

<configuration>
<appSettings>
<add key= "connstr1" value= "Data source=.;i Nitial catalog=dbname;integrated security=true "/>
<add key= "connstr2" value= "..." >
</appSettings>
<connectionStrings>
<add name= "CONNSTR3" connectionstring= "..."/>
<add name= "CONNSTR4" connectionstring= "..." providername= " System.Data.Sqlclient"/>
</connectionStrings>
</configuration>

As shown in the code above: both <appSettings> and <connectionStrings>.

2, the following two ways to analyze:

AppSettings:

① It is used in the asp.net1.1, in the vs2003.

The ② is the equivalent of a key-value pair in the form, key and value. Not only can you save the connection string, but you can also store some configuration items.

③ in appsettings, you can not use Providername= "System.Data ..." (but if you want to use it, just write it in value and pass it as a value).

④ in the background to take the value way: string conn=system.configuration.configurationmanager.appsettings["ConnStr";]

ConnectionStrings:

① It is new in asp.net2.0.

The ② is also similar to the key-value pair, using name and ConnectionString, which generally saves the connection string.

③ can use providername in connectionstrings.

④ the way the value is taken in the background: string conn=system.configuration.configurationmanager.connectionstrings["ConnStr"]. ConnectionString;

3, the difference between the two ways:
ConnectionStrings is the 2.0 release, and it has more benefits than appsettings:

① can encrypt the connection string and use one of MS's encryption tools.

② can bind directly to a data source control without having to write code to read it and assign it to the control.

③ can easily replace the database platform, such as the Oracle database, just modify the providername.

4. What is the role of providername?

Let's take a look at the parameter values of the providername.

①providername= "System.Data.SqlClient"----instructions for using the MSSQLSERVER database
②providername= "System.Data.SqlLite"----instructions for using the Sqllite database
③providername= "System.Data.OracleClient"----instructions for using an Oracle database
or providername= "System.Data.Oracle.DataAccess.Client"----Ibid.
④providername= "System.Data.OleDb"----instructions for using an Access database

Note: ProviderName can be written without writing.

5. When do we use providername?

For example, we are now going to do a project that will be sold for two companies using: A and B. So there's an element of uncertainty, what if a uses Oracle and B uses SQL Server? So:

① database: We need to build two libraries, one with Oracle and one with SQL Server.

② Program: We generally do not write two systems to let them use, we will definitely determine what database they are using, and then execute what kind of database script in the program.

The ③web.config code is as follows:

<configuration>
<connectionStrings>
<add name= "ConnStr" connectionstring= "Data source=.;i Nitial catalog=mydb;integrated security=true "providername=" System.Data.SqlClient "/>
</connectionStrings>
</configuration>

The ④ program code is as follows:now make a judgment, if Providername= "System.Data.SqlClient" executes SQL Server script,and if Providername= "System.Data.OracleClient" calls Oracle's database script, the code is as follows:
1  Public Static ReadOnly stringConnStr = system.configuration.configurationmanager.connectionstrings["ConnStr"]. ProviderName;2  Public Static stringDatabaseType = system.configuration.configurationmanager.connectionstrings["ConnStr"]. ProviderName;3  Public Static intExecuteNonQuery (CommandType commandtype,stringCommandText,paramssystem.data.oledb.oledbparameter[] parm)4 {5     intnum =0;6     if(DatabaseType = ="System.Data.SqlClient")7     {8         //database scripts for Microsoft SQL Server are executed here9     }Ten     Else if(DatabaseType = ="System.Data.OracleClient") One     { A         //Oracle Database scripts are executed here -     } -     returnnum; the}

Back to Top

DS02: The contents of the database connection string

1, background: We write a program to access the SQL Server database using the connection string, the connection string tells the program which database server to connect to, which instance, which database, and how the database login authentication method is information.

These three are the most basic, so the connection string at least by the above three elements, and other depending on the circumstances of the use of what to add.

2, although the simplest database connection string consists of only three elements, but there are many ways to write, we first look at the simplest connection string writing format:

@ "Data source=xl-pc; Initial catalog=db_test; Uid=sa; pwd= Test "

@ "Data source=xl-pc; Initial catalog=db_test; Integrated Security=true "

3, the above is the two simplest connection string, now let us specifically analyze:

(1)DataSource: Specifies the name of the data server to which you want to connect. The writing format is typically "data server name/instance name".

DataSource can also be replaced with SERVER,ADDRESS,ADDR. If the database server is local, the entry can be written "." Directly. or "(local)" does not include quotation marks.

If you have more than one database instance installed locally, write "." Directly. or "(local)" will automatically connect to the higher version of the instance.

Local non-Express version of the database server address is the same as the database instance name, you can directly write database server name;

The local express version of the DB instance name generally defaults to "computer name +/sqlexpress" such as "xl-pc/sqlexpress" or "(local)/sqlexpress" or "./sqlexpress";

If the remote server is connected, the format is "IP address +/instance name".

There must be only one space between data and source. Case insensitive except for passwords. All punctuation marks are English punctuation.

(2)Initial Catalog: Specifies the name of the database to connect to.

data specifications are the same as DataSource, and can also be replaced with database.

(3)UID is user name,PWD is password, the two can be replaced by UserID and password respectively.

(4)Integrated Security: Attribute set to TRUE indicates Windows authentication and does not require a user name and password to be specified.

Back to Top

January 19, 2015 13:26:59

by Selonsy

Some summary of the Web. config connection string

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.