Teaching experience: ADO. NET connection and Disconnect type

Source: Internet
Author: User
Tags count
ado| Disconnect

A lot of books and articles about ado.net, here mainly use in my teaching to give students a demonstration of two small examples, to compare the ado.net connection and disconnection, programmers generally do not like preaching, the following code to speak:

Connection type:

SqlConnection sqlconn=new SqlConnection ("server=.; Database=pubs;user id=sa;password=; ");
SqlCommand sqlcomm=new SqlCommand ("select * from authors", sqlconn);
Operation between opening and disconnecting a database
Sqlconn.open ();
SqlDataReader Dr=sqlcomm.excutereader ();
while (Dr. Read ())
{
for (int i=0; i<dr. FieldCount; i++)
{
Console.Write (Dr. GetValue (i). ToString () + "");
}
Console.WriteLine ();
}
Dr. Close ();
Sqlconn.close ();

Disconnect Type

SqlConnection sqlconn=new SqlConnection ("server=.; Database=pubs;user id=sa;password=; ");
SqlDataAdapter Adapter=new SqlDataAdapter ("select * from authors", sqlconn);
Used to automate production update commands
SqlCommandBuilder cb=new SqlCommandBuilder (adapter);
Sqlconn.open ();
DataSet ds=new DataSet ();
Adapter. Fill (DS);
Sqlconn.close ();
Processing data after opening and closing
for (int i=0; i<ds. Tables[0]. Rows.Count; i++)
{
for (int j=0; j<ds. Tables[0]. Columns.count; J + +
{
Console.Write (ds. Tables[0]. rows[i][j]+ "");
}
Console.WriteLine ();
}
Change data
Ds. Tables[0]. Rows[0][1]= "A";
Ds. Tables[0]. ROWS[1]. Delete ();
Update Database
Sqlconn.open ();
Adapter. Update (DS);
Sqlconn.close ();




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.