Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the analysis of ASP.NET database connection string

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you the analysis of the ASP.NET database connection string. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Use OleDbConnection objects to connect to OLE DB data sources

1. Connect to 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: Access database only provides two connection properties, provider (data provider) and data source (data source).

The file format for Access2000\ 2003 is ".mdb" and the file format for Access2007 is ".accdb"

The data provider version of Access is backward compatible, testing under Win7 using the Microsoft.Jet.OLEDB.3.5 prompt "the Microsoft.Jet.OLEDB.3.5 provider is not registered on the local computer." You can access Access2000's database files by using Microsoft.Jet.OLEDB.4.0 or Microsoft.Ace.OLEDB12.0 instead. Of course, you can also try to use MDAC provided by Microsoft to modify the version of provider.

2. Connect to 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 code, the table name should be represented as "[worksheet name $]". When a field is reserved for the database, add [] to the field name to show the difference. For example, when defining a select statement: string connStr= "select * from [login$] where username='abc' and [password] = 'abc123'".

If you use a number as text type data in a datasheet, you should put single quotation marks before the number to force the default value to the text type.

3. Connect to SQL Server database

Provider=SQLOLEDB; Data Source= server name; Initial Catalog= database name; uid= user; pwd= password

Second, use SqlConnection object to connect to SQL Server database

Declaration: the properties of the following connections can be aliased by referring to the SQL Server database connection string parameter list; other auxiliary properties can be set in addition to the properties that must be set. Such as Connect Timeout, Encrypt, etc.

How to set the path of the database file:

1. Use the 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"

The third method is recommended. "| DataDirectory |" represents the App_Data folder automatically created in the ASP.NET project.

1. Connect to SQLServer in SQLServer authentication mode

(1) connect with the database name

Server= server name; Database= database name; User ID= user name; Password= password

Or (use abbreviations and aliases)

Server= server name; Initial Catalog= database name; Uid= user; Pwd= password

(2) connect to the full path of the database file

"Serve= server name; AttachDbFilename= database file path; User ID= user name; 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 empty.

2. Connect to SQL Server in Windows authentication mode

(1) connect with the database name

Server= server name; Database= database name; Integrated Security=SSPI

(2) connect to the full path of the database file

"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 "

Remarks: SSPI is true.

Third, use OdbcConnection objects to connect to ODBC data sources

"Driver= database provider name; Server= server name; Database= database name; Trusted_Connection=yes"

Example:

First of all, configure the corresponding data source in the computer management à data source à (select the database type, set the database file path and the corresponding database name)

Driver= Microsoft.Jet.OLEDB.4.0; Server=.\ SQLEXPRESS; Database=DatabaseName; Trusted_Connection=yes

Fourth, use OracleConnection object to connect to Oracle database

Data Source=Oracle8i; Integrated Security=yes

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

1. Add a connection to the tag

Or

Get the connection string in the tag in the program code:

Reference Namespace:

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

2. Add a connection to the tag

Or

Get the connection string in the tag in the program code:

Reference Namespace:

Using System.Configuration; string connStr = ConfigurationManager.AppSettings ["ConnectionName"]. ToString (); this is what the analysis of the ASP.NET database connection string shared by the editor is like. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report