Clear Asp.net (8): Connection of ADO. net

Source: Internet
Author: User
Tags sql server connection string connectionstrings

The connection class is used to establish a connection with the data source to be interacted.

1. connection string

Each connection should contain the following elements: Server location, database name, authentication method, and database provider

For example, the following SQL server connection string.

1 string connstr = "Data Source = localhost; initial catalog = mydemo; user id = sa; Password = sqlserver2008 ";

You can define the connection string in Web. config as follows:

1 <connectionstrings>
2 <Add name = "mydemo" connectionstring = "Data Source = localhost; initial catalog = mydemo; user id = sa; Password = sqlserver2008"/>
3 </connectionstrings>

Then retrieve

1 string connstring = configurationmanager. connectionstrings ["mydemo"]. connectionstring;

Ii. Status of the connection class

You can obtain the status of the connection object from the state attribute of the connection object.

For example, SQL Server data provider .) :

1 <asp: Label id = "lblinfo" runat = "server" text = ""> </ASP: Label>

 

C #

1 protected void testconnection1 ()
2 {
3 string connstring = configurationmanager. connectionstrings ["mydemo"]. connectionstring;
4 sqlconnection con = new sqlconnection (connstring );
5 using (CON)
6 {
7 con. open ();
8 This. lblinfo. Text = "<B> server vision: </B>" + con. serverversion;
9 This. lblinfo. Text + = "<br/> <B> connection is: </B>" + con. state. tostring ();
10}
11 This. lblinfo. Text + = "<br/> <B> now connection is: </B>" + con. state. tostring ();
12}

 

Iii. Connection connection statistics

Considering the performance, connection statistics are disabled by default. Statistics are started only when the sqlconnection. statisticenabled attribute is true.

Call retrievestatistics () to obtain statistics. The statistical results are provided through a collection of loose names/values.

Call resetstatistics () to clear the statistical result and start again.

Example:

1 <asp: Label id = "lblstatistics" runat = "server" text = ""> </ASP: Label>

 

C #

1 protected void testconnection ()
2 {
3 string connstring = configurationmanager. connectionstrings ["mydemo"]. connectionstring;
4 sqlconnection con = new sqlconnection (connstring );
5 con. statisticsenabled = true;
6
7 try
8 {
9 con. open ();
10 This. lblinfo. Text = "<B> server vision: </B>" + con. serverversion;
11 This. lblinfo. Text + = "<br/> <B> connection is: </B>" + con. state. tostring ();
12}
13 catch (exception ERR)
14 {
15 this. lblinfo. Text = "error reading the database." + err. message;
16}
17 finally
18 {
19 con. Close ();
20 This. lblinfo. Text + = "<br/> <B> now connection is: </B>" + con. state. tostring ();
21
22 idictionary statistics = con. retrievestatistics ();
23 // The total number of bytes obtained from the server
24 lblstatistics. Text + = "<br/> <B> bytesreceived: </B>" + statistics ["bytesreceived"]. tostring ();
25 // Number of requests for connection
26 lblstatistics. Text + = "<br/> <B> serverroundtrips: </B>" + statistics ["serverroundtrips"]. tostring ();
27 // connection time
28 lblstatistics. Text + = "<br/> <B> connectiontime: </B>" + statistics ["connectiontime"]. tostring ();
29 // number of queries executed
30 lblstatistics. Text + = "<br/> <B> sumresultsets: </B>" + statistics ["sumresultsets"]. tostring ();
31 // all rows affected by the query
32 lblstatistics. Text + = "<br/> <B> selectrows: </B>" + statistics ["selectrows"]. tostring ();
33}
34}

 

Clear Asp.net series learning blog directory

 

 

Reference: Pro ASP. NET 3.5 in C #2008

 

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.