ASP. NET data providers work in two ways

Source: Internet
Author: User

 

1 using System;
2
3 // contains the basic objects and public classes used to access and store relational data, such as DataSet, DataTable, and DataRelation.
4 using System. Data;
5 using System. Collections. Generic;
6 using System. Linq;
7 using System. Web;
8 using System. Web. UI;
9 using System. Web. UI. WebControls;
10 using System. Data. SQL;
11
12 // contains public classes used to connect and manipulate SQLServer databases, such as SqlConnection and SqlCommand
13 using System. Data. SqlClient;
14 /// <summary>
15 // two ways of working for the data provider
16 /// </summary>
17 public partial class _ Default: System. Web. UI. Page
18 {
19 protected void Page_Load (object sender, EventArgs e)
20 {
21 /***
22 * use the Connection object, Command object, and DataReader object to process data
23 *
24 **/
25
26 // construct the Connection object used to establish the Connection
27 SqlConnection conn1 = new SqlConnection ();
28
29 // set the connection string attribute ConnectionString of the connection object
30 conn1.ConnectionString = "Data Source = SHERRY; Initial Catalog = test; Integrated Security = True ";
31
32 // construct the Command object
33 SqlCommand cmd1 = new SqlCommand ();
34
35 // set the CommandText attribute of the command object
36 bytes 1.commandtext = "select * from [user]";
37
38 // set the Connection attribute of the command object, which indicates the Connection to which the command object sends the command
39 rows 1.connection = conn1;
40
41 // open the database connection
42 conn1.Open ();
43
44 // execute the command and direct the returned result to the DataReader object
45 SqlDataReader reader = pai1.executereader ();
46
47 // use the GridView control to display data
48 GridView1.DataSource = reader;
49 GridView1.DataBind ();
50
51 // close the connection
52 conn1.Close ();
53
54 /***
55 * use a DataAdapter object and a DataSet object to process data
56 *
57 **/
58
59 // define the data adapter object
60 SqlDataAdapter adapter = new SqlDataAdapter ();
61
62 // define a DataSet object
63 DataSet dsDataSet = new DataSet ();
64
65 // define the data connection object and set the connection string attributes
66 SqlConnection conn2 = new SqlConnection ();
67 conn2.ConnectionString = "Data Source = SHERRY; Initial Catalog = test; Integrated Security = True ";
68
69 // define the command object and set relevant attributes
70 SqlCommand cmd2 = new SqlCommand ();
71 .. commandtext = "select * from [user]";
72 connecting 2.connection = conn2;
73
74 // set the query command to the SelectCommand attribute of the data adapter object
75 adapter. SelectCommand = cmd2;
76
77 // define a able object
78 DataTable table = new DataTable ();
79
80 // Fill the dataset with the Fill method of the data adapter object
81 adapter. Fill (dsDataSet, "table ");
82
83 // put in the able
84 table = dsDataSet. Tables ["table"];
85
86 // output the default view of the able in DataSet
87 this. GridView2.DataSource = table. DefaultView;
88 this. GridView2.DataBind ();
89}
90
91}
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.