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 ();