ASP. NET database connection process

Source: Internet
Author: User

Read the other People's blog, personal summary, there are errors please point out

1. Establish database Connection object

2, set up Database query object (increase and deletion of search)

3. Set up DataSet data Table object to save data and to realize disconnection

4. Set up Database Operation object

5. Get the connection string used to connect to the database by Conn object

6. Initialize the Query object

7. Operation of SQL statement by database operation Object

8. Initialize the DataSet object

9. Open the connection, get the data, close the connection

10. Throw an exception

protected Sqlconnection Conn;

Protected SqlDataAdapter da;

protected DataSet ds;

protected DataCommand Comm;

protected void Page_Load (object sender, EventArgs e)
{
Conn=new SqlConnection ("Data source=localhost;initial catalog=nd_data; User Id=sa; Password=aaaaaa ");//Fetch the connection string while establishing the connection
Da= New SqlDataAdapter ();//Initialize Query object
Da. selectcommand= New SqlCommand ("Select Name,id from xs Order by Id,name DESC", conn); database operation with a query ID and name
ds= new DataSet (); Initialize DataSet object
Try
{
Conn. Open ();//Opening connection
Da. Fill (ds, "abs");//Get the data and store it in a table named "ABS"
Conn. Close ();//Closed connection
}
catch (SqlException E1)//error handling
{
Response.Write (E1. ToString ());
}

Data Display phase:

PagedDataSource objpds= New PagedDataSource ();//Create a data source object that acts on the control

objpds.datasource= ds. Tables["ABS"]. defaultview;//the "ABS" table that was saved before passing in
Datalistname.datasource= objpds;//A data source object into the DataList control
Datalistname.databind ();//datalist control displays data information

Foreground data Display method:

<itemtemplate>//datalist Data Control Templates
<asp:label id= "Lbnwes" runat= "server" text= ' <% #Eval ("id")%> ' ></asp:label>//display ID
<asp:label id= "Lbtime" runat= "server" text= ' <% #Eval ("name")%> ' ></asp:label>//display name
</ItemTemplate>

ASP. NET database connection process

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.