ASP. net c # various database connection strings-sqlserver, Oracle, and access

Source: Internet
Author: User

When I first started to study C # development projects, I had to worry about writing a secure and efficient database connection for a long time. I found that many friends who have just started learning and some who have been friends for a year or two are also a little worried. I will post this code and give myself a reference in the future, it also serves as a learning reference for friends.
I have summarized the commonly used database connection strings in the. NET project: sqlserver, Oracle, and access databases (MySQL, sqllite, Excel, HTML table, and so on ).

1. Description of common connection string Parameters

For more information, see:
Sqlconnection. connectionstring
Oracleconnection. connectionstring
Oledbconnection. connectionstring

Keywords Default Description
Server or data source N/ Name or network address of the database instance to be connected (you can specify the port number after the name), specify the local instance availability (local), if it is sqlexpress (name \ sqlexpress ).
Initial catalog or database N/ The name of the database.
User ID or uid N/ Log on to your account.
Password or pwd N/ Account Logon password.
Persist Security info 'False' When this value is set to false or no (strongly recommended), if the connection is enabled or is always in the open state, then the security sensitive information (such as the password) will not be returned as part of the connection. Resetting the connection string resets all connection string values, including passwords. The identifiable values are true, false, Yes, and no.
Enlist 'False' True indicates that the connection pool program automatically registers the connection in the context of the current transaction in which the thread is created. The identifiable values are true, false, Yes, and no.
Connection lifetime 0 When the connection is returned to the pool, the creation time is compared with the current time. If the length of time (in seconds) exceeds
The connection is destroyed. This is useful in clustering configuration (used to force load balancing between running servers and servers that are just online ). Zero (0)
The maximum timeout value for the pool connection.
Max pool size 100 The maximum number of connections allowed in the pool.
Min pool size 0 The minimum number of connections allowed in the pool.
Pooling 'True' When this value is true, the system extracts the dbconnection object from the appropriate pool, or creates the object and adds it to the appropriate pool as needed. The identifiable values are true, false, Yes, and no.
Packet Size 8192 The size of network data packets used to communicate with SQL Server instances, in bytes.
Attachdbfilename, extended properties, or initial file name N/ The name of the master database file, including the complete path name that can be connected to the database. Only primary data files with. MDF Extensions support attachdbfilename.
If the primary data file is read-only, the additional operation fails.
This path can be an absolute or relative path, depending on whether to use datadirectory to replace the string. If datadirectory is used, the corresponding database file must exist in the subdirectory to which the replacement string points.
Note: Remote Server, HTTP, and UNC path names are not supported.
Instance: you must use the keyword "Database" (or one of its aliases) to specify the database name as follows:
"Attachdbfilename = | datadirectory | \ data \ yourdb. MDF; Integrated Security = true; database = yourdatabase"
If the log file exists in the directory where the data file is located and the "Database" keyword is used when the primary data file is appended, an error is generated. In this case, remove the log file. After a database is attached, the system automatically generates a new log file based on the physical path.
Integrated Security or trusted_connection 'False' If the value is false, the user ID and password are specified in the connection. If this parameter is set to true, the current Windows Account creden。 are used for authentication. The identifiable values are true, false, yes, no, and sspi equivalent to true (strongly recommended ).

Ii. Common connection string instances

For more instances, see database connection string instances.

Sqlserver simple connection: Server = server address; database = database name; user id = username; Password = password; local file trusted connection: Server =. \ sqlexpress; attachdbfilename = | datadirectory | mydbfile. MDF; database = database name; trusted_connection = sspi; sqlserver custom connection: Data Source = (local); initial catalog = database name; user id = user name; Password = password; persist Security info = true; enlist = true; Max pool size = 300; min pool size = 0; connection lifetime = 300; packet size = 1000; Oracle simple connection: dat A source = orclsid_127.0.0.1; user id = username; Password = password; // the data source is found in the tnsnames. ora file in the Oracle installation directory. Instead of finding it in "Data Source (ODBC)" under "Administrative Tools" of the system. The tnsnames. ora file is under "client_1/Network/admin/" under the Oracle installation directory. Oracle custom connection: Server = (description = (address_list = (address = (Protocol = TCP) (host = server address) (Port = port ))) (CONNECT_DATA = (Server = dedicated) (SERVICE_NAME = Database Name); User ID = user name; Password = password; persist Security info = true; enlist = true; max pool size = 300; min pool size = 0; connection lifetime = 300; Access simple connection: provider = Microsoft. jet. oledb.4.0; Data Source = c: \ mydatabase. MDB; user id = user name; Password = password; provider = Microsoft. jet. oledb.4.0; Data Source = c: \ mydatabase. MDB; Jet oledb: Database Password = password;

I only list some common connection string instances and parameter descriptions. I was originally preparing to elaborate on the instance and parameter descriptions of the connection string based on each database. I did not do so later, because this post is mainly intended to meet the needs of friends who just started learning, because it can basically meet the needs of most projects. If you want a project with high concurrency, You need to optimize and transform the project, database, server, and so on. I will not elaborate on it here.

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.