Ado. NET Getting Started Tutorial (c) connection string, did you underestimate it?

Source: Internet
Author: User
Tags db2 connection string mysql connection string sql server express connectionstrings

Summary

Ado. NET is a powerful advantage in providing consistent access to different data sources. In the previous article, "What you have to know about ADO (ii) about the. NET data provider," we know. NET uses different data providers for different data sources, which allows us to access the appropriate data sources more efficiently. Apart from. NET data provider, I have to say another hero: the connection string (Connection Strings), which also contributed a very important force to ADO in resolving the contradiction between "different" and "consistent".

Directory
    • What is a connection string?
    • Understanding Syntax Formatting
    • Examples of several typical connection strings
    • How do I construct a connection string?
    • To store the connection string in a configuration file
1. What is a connection string?

We already know that ADO. NET class Library provides consistent access to different external data sources. These data sources can be local data files (such as Excel, TXT, access, or even SQLite) or remote database servers (such as SQL Server, MySQL, DB2, Oracle, and so on). The data sources seem to be dazzling, mixed. Just imagine, ADO. How can you access different data sources accurately and efficiently? In the previous article, "You Must know ADO (ii) understand. NET data provider," You may already know that ADO. NET has written different data providers for different data sources. But the premise is that we have access to the right data source. Otherwise, will only "pigtailed, donkey head not horse mouth". Just like, we use the SQL Server data provider to process the Excel data source, the result must be people "jaw-dropping". Heroes always appear when they are most needed, and the connection string is a set of formatted key-value pairs: It tells the ADO where the data source is, what data format is required, what level of access trust is provided, and any other information that includes the connection. Oh, yes! If you think so, then you can use the connection string later, you may not underestimate it!

2. Understanding the syntax format

In fact, the connection string, although far-reaching, but its own syntax is very simple. A connection string consists of a set of elements, an element containing a key-value pair, between elements by ";" Separate. The syntax is as follows:

Key1=value1;key2=value2;key3=value3 ...

Typical elements (key-value pairs) should contain this information: whether the data source is a file-based or Web-based database server, if an account password is required to access the data source, what is the limit for timeouts, and other relevant configuration information. We know that the value is determined by the key (key), so how is the key determined? The syntax does not specify what the key is, which needs to be determined based on the data source you need to connect to. In the next section, I'll cover several common connection strings in more detail.

Tip: If you need to know more about the format of the connection string, you can refer to http://www.connectionstrings.com/.

3. Example of several typical connection strings 3.1 SQL Sever connection string

(1) Standard safety connection

Data source=myserveraddress;initial catalog=mydatabase; User Id=myusername; Password=mypassword;

Description

Data Source: The server that needs to be connected. It is important to note that if you are using the Express version of SQL Server, you need to add \sqlexpress after the server name. For example, a database server that connects to a local SQL Server Express version can be written as data Source = (local) \SQLExpress or. \SQLExpress.

Initial Catalog: The name of the database used by default.

User ID: Database server account.

Password: Database server password.

Or it can be written like this:

Server=myserveraddress;database=mydatabase; User Id=myusername; Password=mypassword; Trusted_connection=false;

(2) Trusted connection

Data source=myserveraddress;initial catalog=mydatabase;integrated Security=sspi;

Description

Data Source: Same as above.

Initial Catalog: Same as above.

Integrate security: Access the database using the Windows secure certificate that exists.

Or it can be written like this:

Server=myserveraddress;database=mydatabase; Trusted_connection=true;

3.2 Access connection string
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\mydatabase.mdb; User id=admin; password=;

3.3 MySQL Connection string
Server=myserveraddress;database=mydatabase; Uid=myusername; Pwd=mypassword;

3.4 DB2 Connection string
Server=myaddress:myportnumber;database=mydatabase; Uid=myusername; Pwd=mypassword;

3.5 Oracle Connection string
Data SOURCE=TORCL; User Id=myusername; Password=mypassword;

4. How do I construct a connection string?

In the above we know that the connection string is essentially a string, so we can fully use the

String connstr = "Data source=myserveraddress;initial catalog=mydatabase; User Id=myusername; Password=mypassword ";

To construct a connection string. In fact, ADO. NET has a dedicated class to handle the connection string: Dbconnectionstringbuilder. the Dbconnectionstringbuilder class generates a base class for strongly typed connection strings . The reason to have such a class is because it is more secure and friendly. In SQL Server, for example, you can build a connection string like this:

1 Sqlclient.sqlconnectionstringbuilder builder =
2 new Sqlclient.sqlconnectionstringbuilder ();
3 Builder. DataSource = @ "(local) \SQLExpress";
4 Builder. InitialCatalog = "MyDataBase";
5 Builder. IntegratedSecurity = true;

5. Storing the connection string in the configuration file

In our actual development, we generally do not write the connection string directly in the code, but rather stored in the configuration file. Writes the connection string to die in the code, is inconvenient to maintain, each time modifies the string, also must recompile the code. As an example of an ASP. NET application, we typically write the connection string to the <connectionstrings/> node of the Web. config configuration file. For example:

1 <connectionStrings>
2 <add name= "connstr" connectionstring= "Data source=.\sqlexpress;initial catalog=mydatabase;integrated Security=sspi "/>
3 </connectionStrings>

Therefore, we just need to add the code in the program to get the values in the configuration file, such as:

Of course, you can add a connection string under the <appsettings/> node of the configuration file, and get a string value in a similar way. In the next article, I will explain how to connect to the database, I hope you have a lot of attention.

Article posted: http://liuhaorain.cnblogs.com

Ado. NET Getting Started Tutorial (c) connection string, did you underestimate it?

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.