Ado. NET learning notes-linking to the data Store

Source: Internet
Author: User
Tags connectionstrings

1. Moving data using the data provider (Providers). NET contains the following 4 types of data providers by default:
    • OLE DB
    • Odbc
    • SQL Server
    • Oracle
In addition, third-party data providers such as MySQL and DB2 can be used. The primary function of the data provider is to move data between the local program and the remote Data Store.2.DbConnection ObjectsTo get the data, you first have to have a legally available data link (Connection), the abstract class for the data link is dbconnection, and then the different inheritance of the data provider generates the corresponding specific data link class, such as SqlConnection (SQL Server), Mysqlconnection (MYSQL) and so on. The sample code for opening/closing a link is as follows (SQL SERVER):
var connection = new SqlConnection (); connection. ConnectionString =   "server=.;D Atabase=northwind; Trusted_connection=true "; Connection. Open ();   Do lots of the cool work hereconnection. Close ();
There are two common ways to close data links, one is to use the close () method, the other is to use a using statement block, which is generally considered better, one is that you can close the link without displaying the call Close () method (often forgetting), and the second is if there is an exception thrown, Even if the close () statement cannot be enforced, the link can still be closed normally.3. ConnectionString PropertiesSetting the ConnectionString property is one of the hardest links to build a link, and fortunately we have the VS tool to generate the link string intuitively. After we set up the link string, we typically save it on our local computer so that we don't have to compile the program again, even if the link string changes. In the example program, the app. config file contains the link string content as follows: <?xml version= "1.0" encoding= "Utf-8"?><configuration> < configsections> </configSections> <connectionStrings> <add name= "TestApp.Properties.Setting S.toucaiconnectionstring "connectionstring= "Server=mytoucai.mysql.rds.aliyuncs.com;user id=lqs2011;password = gdz840928; Persistsecurityinfo=true;database=toucai "Providername= "MySql.Data.MySqlClient"/> </connectionStrings> <startup> <supportedruntime v ersion= "v4.0" sku= ". netframework,version=v4.5 "/> </startup></configuration> read the link string from the file and open the link in the sample code as follows: using System.Configuration; static void Main () {var constring = configurationmanager.connectionstrings["TestApp.Properties.Settings.toucaiConnectionString"]. ConnectionString; var conn = new Mysqlconnection (constring); var cmd = conn.            CreateCommand ();            Cmd.commandtype = CommandType.Text;                       Cmd.commandtext = "SELECT count (*) from product"; Conn.            Open (); The Long Count = (long) cmd.            ExecuteScalar ();            Console.WriteLine ("Total" + count + "article item record"); Conn.                   Close (); When this code executes, the program throws an exception if the contents of the APP.CONIFG file, such as the property value of connectionstring or name, are set incorrectly. It is also important to note that you need to add a reference to the system.configuration instead of just the using statement.4. Encrypted communication to MySQLThe content of this aspect also needs to be consulted, but it is very important.5. Link PoolConsider the overhead of suggesting a physical link to the database, ADO. NET uses the link pooling mechanism to reuse previously established links. The system default link pool settings are sufficient, generally we do not have to set up their own. The link pool is located on the client, not the server side. The life cycle of links in a linked pool is limited, usually a few minutes, and you can set the Min Pool Size property to n when you find a bug, you can temporarily close the link pool (pooling = False in the link string) after the server restarts, if you want to ensure that the links in the link pool are at least n useful. Links in the link pool may be invalidated, so we need to run DbConnection's static method Clearpool () or clearallpools () to empty the links in the link pool to avoid throwing exceptions.

Ado. NET learning notes-linking to the data Store

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.