Summary of ASP. NET database connection strings

Source: Internet
Author: User

About DatabasesLink stringWe have introduced a lot of related knowledge. We suggest you read these two articles, "Learn more about SQL Server connection strings" and "MySql connection strings" for your reference. The following describesASP. NETDatabase connection string Summary

1. Use OleDbConnection object to connect to the ole db Data Source

1. Connect to the Access Database

Access 2000: "provider = Microsoft. Jet. Oledb.3.5; Data Source = Access file path"

Access 2003: "provider = Microsoft. Jet. Oledb.4.0; Data Source = Access file path"

Access 2007: "provider = Microsoft. Ace. Oledb.12.0; Data Source = Access file path"

Note: The Access database only provides two Connection Properties provider data providers) and data source data sources );

Access2000 \ 2003 is in the format of ". mdb", and Access2007 is in the format of ". accdb ";

The data provider version of Access is backward compatible, and Microsoft is used for testing in Win7. jet. OLEDB.3.5 prompts "Microsoft. jet. OLEDB.3.5 "provider. ", Use Microsoft. Jet. OLEDB.4.0 or Microsoft. Ace. OLEDB12.0 to access the database file of Access2000. Of course, you can also try to Use MDAC provided by Microsoft to modify the provider version.

2. Connect to the Excel database

Excel 2003: "provider = Microsoft. Jet. OLEDB.4.0; Data Source = Access file path; extended properties = excel 8.0"

Excel 2007: "provider = Microsoft. Ace. OLEDB.12.0; Data Source = Access file path; extended properties = excel 12.0"

Note: When referencing a worksheet in the Code, the table name should be expressed as "[worksheet name $]". When a field is reserved for the database, add [] to the field name to indicate the difference, for example, when defining a select statement: string connStr = "select * from [login $] where username = 'abc' and [password] = 'abc123 '";
If numbers are used as text data in the data table, the default value should be forcibly set to text type with single quotation marks before the number.

3. Connect to the SQL Server database

 
 
  1. Provider = SQLOLEDB;
  2. Data Source = server name;
  3. Initial Catalog = database name;
  4. Uid = user;
  5. Pwd = Password

2. Use the SqlConnection object to connect to the SQL Server database

Declaration: For the following connection attributes, refer to "SQL Server database connection string parameter list" to obtain its alias. In addition to the attributes that must be set, you can also set other auxiliary attributes. Such as Connect Timeout and Encrypt

To set the path of a database file:

1. Use absolute path: "AttachDbFilename = D: \ Solution1 \ Web \ App_Data \ data. mdf"

2. Use the Server relative path: "AttachDbFilename =" + Server. MapPath ("\ App_Data \ data. mdf ")

3. Use the simplest relative path: "AttachDbFilename = | DataDirectory | \ data. mdf"

We recommend that you use the following 3rd Methods: "| DataDirectory |" indicates the App_Data folder automatically created in the ASP. NET project.

1. Connect to SQLServer in SQL Server Authentication Mode

1) connection by database name

 
 
  1. Server = Server name;
  2. Database = Database name;
  3. User ID = User name;
  4. Password = Password

Or use the abbreviation and alias)

 
 
  1. Server = Server name;
  2. Initial Catalog = database name;
  3. Uid = user;
  4. Pwd = Password

2) use the full path of database files for connection

"Serve = server name; AttachDbFilename = database file path; User ID = username; Password = Password"

Example:

 
 
  1. Server=.\SQLEXPRESS; Database=DatabaseName; User ID =sa; Password=abc123”  
  2. Server=.\SQLEXPRESS; Initial Catalog =DatabaseName; Uid =sa; Pwd=abc123”  
  3. Server=(local)\SQLEXPRESS; AttachDbFilename=D:\\Solution1\\Web\\App_Data\\data.mdf;
  4. User ID =sa; Password=abc123” 

Note: The password can be blank.

2. Connect to SQL Server in Windows Authentication Mode

1) connection by database name

 
 
  1. Server = Server name;
  2. Database = Database name;
  3. Integrated Security = SSPI

2) use the full path of database files for connection

"Serve = server name; AttachDbFilename = database file path; Integrated Security = true"

Example:

 
 
  1. Server = Server name;
  2. Database = Database name;
  3. Integrated Security = SSPI
  4. Server = (local) \ SQLEXPRESS;
  5. AttachDbFilename = D :\\ Solution1 \ Web \ App_Data \ data. mdf;
  6. Integrated Security = true"

Note: SSPI is true.

3. Use the OdbcConnection object to connect to the ODBC Data Source

"Driver = Database provider name; Server = Server name; Database = Database name; Trusted_Connection = yes"
Example:

First, you must select the database type for the corresponding data source configuration in Computer Management, and set the database file path and the corresponding database name)

 
 
  1. Driver= Microsoft.Jet.OLEDB.4.0;  
  2. Server=.\SQLEXPRESS;   
  3. Database=DatabaseName;  
  4. Trusted_Connection=yes 

4. Use the OracleConnection object to connect to the Oracle database

 
 
  1. Data Source=Oracle8i;  
  2. Integrated Security=yes 

5. Configure the database connection in the web. config file of the ASP. NET project and obtain the connection string in the program code.

1. Add a connection in the <connectionStrings> tab.

 
 
  1. <connectionStrings>   
  2. <add name="ConnectionName" connectionString="Server=.\SQLEXPRESS;Database=DatabaseName;
  3. User ID=sa;Password=abc123"   
  4. providerName="System.Data.SqlClient" />   
  5. </connectionStrings> 

Or

 
 
  1. <connectionStrings>   
  2. <add name="ConnectionName" 
  3. connectionString="Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\data.mdf;
  4. Integrated Security=true" 
  5. providerName="System.Data.SqlClient" />   
  6. </connectionStrings> 

Obtain the connection string in the <connectionStrings> tag in the program code:

Reference namespace:

 
 
  1. Using System.Configuration ;  
  2. string connStr = ConfigurationManager.ConnectionStrings["ConnectionName"].ToString(); 

2. Add a connection in the <deleetask> tab.

 
 
  1. <appSettings>  
  2. <add key="ConnectionName" 
  3. value="Server=.\SQLEXPRESS;Database=DatabaseName;User ID=sa;Password=abc123" />  
  4. </appSettings> 

Or

 
 
  1. <appSettings>  
  2. <add key="ConnectionName"   
  3. value="Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\data.mdf;Integrated Security=True" />  
  4. </appSettings> 

Obtain the connection string in the <etettings> tag in the program code:

Reference namespace:

 
 
  1. Using System.Configuration ;  
  2. string connStr = ConfigurationManager.AppSettings["ConnectionName"].ToString();  

Related Article

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.