Oledb mode:
// Use oledbconnection, oledbdataadapter, oledbcommandbuilder, and dataset.
Using system. Data;
Using system. Data. oledb;
Static void main (string [] ARGs)
{
Try
{
Oledbconnection dbconn = new oledbconnection ();
Dbconn. connectionstring = "provider = sqloledb; server = localhost; database = analytic dB; uid = sa; Pwd = 283918788 ;";
Dbconn. open ();
Oledbdataadapter dbadap = new oledbdataadapter (); // declares that the adapter is used to "Connect" the database and Dataset
String sql1_str = "select * from employees ";
Dbadap. selectcommand = new oledbcommand (sql1_str, dbconn); // fill in the data source
Dataset DS = new dataset ();
Dbadap. Fill (DS, "employees"); // fill
For (INT I = 0; I <Ds. Tables ["employees"]. Rows. Count; I ++) // view records in the DS result set
{
Console. writeline (Ds. Tables ["employees"]. Rows [I] [0]. tostring () + "" +
DS. Tables ["employees"]. Rows [I] [1]. tostring () + "" +
DS. Tables ["employees"]. Rows [I] [2]. tostring () + "" +
DS. Tables ["employees"]. Rows [I] [3]. tostring () + "" +
DS. Tables ["employees"]. Rows [I] [4]. tostring () + "" +
DS. Tables ["employees"]. Rows [I] [5]. tostring ());
}
Console. writeline ("-----------------------------------------------------");
/*
Datarow ROW = Ds. Tables ["employees"]. Rows [0]; // modify the first record
Row. beginedit ();
Row [1] = "mali117 ";
Row. endedit (); // end the modification.
Oledbcommandbuilder dbbuilder = new oledbcommandbuilder (dbadap); // automatically generates an SQL statement for updating data, which must have a primary key
Dbadap. Update (DS, "employees"); // update to database
*/
/*
Datarow ROW = Ds. Tables ["employees"]. newrow (); // Add a new record
Row [1] = "newadduser ";
Row [2] = 22;
DS. Tables ["employees"]. Rows. Add (ROW );
Oledbcommandbuilder dbbuilder = new oledbcommandbuilder (dbadap );
Dbadap. Update (DS, "employees"); // update to database
*/
/*
Datarow ROW = Ds. Tables ["employees"]. Rows [1]; // delete a record
Row. Delete ();
Oledbcommandbuilder dbbuilder = new oledbcommandbuilder (dbadap );
Dbadap. Update (DS, "employees"); // update to database
*/
For (INT I = 0; I <Ds. Tables ["employees"]. Rows. Count; I ++)
{
Console. writeline (Ds. Tables ["employees"]. Rows [I] [0]. tostring () + "" +
DS. Tables ["employees"]. Rows [I] [1]. tostring () + "" +
DS. Tables ["employees"]. Rows [I] [2]. tostring () + "" +
DS. Tables ["employees"]. Rows [I] [3]. tostring () + "" +
DS. Tables ["employees"]. Rows [I] [4]. tostring () + "" +
DS. Tables ["employees"]. Rows [I] [5]. tostring ());
}
Dbconn. Close ();
}
Catch (exception E)
{
Console. writeline (E. Message );
}
Console. writeline ("------------------- \ nrunover ");
Console. Read ();
}
Sqlclient mode:
// Use sqlconnection, sqlcommand, and sqldatareader
Using system. Data;
Using system. Data. sqlclient;
Static void main (string [] ARGs)
{
Sqlconnection dbconn = new sqlconnection ();
Try
{
Dbconn. connectionstring = "Server = localhost; database = analytic dB; uid = sa; Pwd = 283918788 ;";
Dbconn. open ();
Sqlcommand sqlcmd = new sqlcommand ();
Sqlcmd. Connection = dbconn;
String sql1_text = "select * from employees ";
Sqlcmd. commandtext = sql1_text;
Sqldatareader DR = sqlcmd. executereader ();
Console. writeline ("connection OK! ");
Console. writeline ("---------------------------------------");
While (dr. Read () // view records in the DS result set
{
Console. writeline (dr. getint32 (0) + "" + dr. getvalue (1) + "" + dr. getvalue (2 ));
}
Console. writeline ("---------------------------------------");
/*
* Call sqlcmd. executescalar () to return the first column of the first row.
* Call sqlcmd. executenonquery () to execute the update, delete, and insert statements.
*/
Dr. Close ();
}
Catch (exception E)
{
Console. writeline (E. Message );
}
Finally
{
If (dbconn. State = connectionstate. open)
Dbconn. Close ();
}
Console. writeline ("connection close! ");
Console. Read ();
}