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
- Provider = SQLOLEDB;
- Data Source = server name;
- Initial Catalog = database name;
- Uid = user;
- 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
- Server = Server name;
- Database = Database name;
- User ID = User name;
- Password = Password
Or use the abbreviation and alias)
- Server = Server name;
- Initial Catalog = database name;
- Uid = user;
- 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:
- Server=.\SQLEXPRESS; Database=DatabaseName; User ID =sa; Password=abc123”
- Server=.\SQLEXPRESS; Initial Catalog =DatabaseName; Uid =sa; Pwd=abc123”
- Server=(local)\SQLEXPRESS; AttachDbFilename=D:\\Solution1\\Web\\App_Data\\data.mdf;
- 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
- Server = Server name;
- Database = Database name;
- Integrated Security = SSPI
2) use the full path of database files for connection
"Serve = server name; AttachDbFilename = database file path; Integrated Security = true"
Example:
- Server = Server name;
- Database = Database name;
- Integrated Security = SSPI
- Server = (local) \ SQLEXPRESS;
- AttachDbFilename = D :\\ Solution1 \ Web \ App_Data \ data. mdf;
- 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)
- Driver= Microsoft.Jet.OLEDB.4.0;
- Server=.\SQLEXPRESS;
- Database=DatabaseName;
- Trusted_Connection=yes
4. Use the OracleConnection object to connect to the Oracle database
- Data Source=Oracle8i;
- 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.
- <connectionStrings>
- <add name="ConnectionName" connectionString="Server=.\SQLEXPRESS;Database=DatabaseName;
- User ID=sa;Password=abc123"
- providerName="System.Data.SqlClient" />
- </connectionStrings>
Or
- <connectionStrings>
- <add name="ConnectionName"
- connectionString="Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\data.mdf;
- Integrated Security=true"
- providerName="System.Data.SqlClient" />
- </connectionStrings>
Obtain the connection string in the <connectionStrings> tag in the program code:
Reference namespace:
- Using System.Configuration ;
- string connStr = ConfigurationManager.ConnectionStrings["ConnectionName"].ToString();
2. Add a connection in the <deleetask> tab.
- <appSettings>
- <add key="ConnectionName"
- value="Server=.\SQLEXPRESS;Database=DatabaseName;User ID=sa;Password=abc123" />
- </appSettings>
Or
- <appSettings>
- <add key="ConnectionName"
- value="Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\data.mdf;Integrated Security=True" />
- </appSettings>
Obtain the connection string in the <etettings> tag in the program code:
Reference namespace:
- Using System.Configuration ;
- string connStr = ConfigurationManager.AppSettings["ConnectionName"].ToString();