C # manipulating simple database records

Source: Internet
Author: User

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

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.