C # fixed database operation templates

Source: Internet
Author: User
Tags ole

C # fixed database operation templates

String Path = application. startuppath + "// db1.mdb ";
Oledbconnection con = new oledbconnection (@ "provider = Microsoft. jet. oledb.4.0; Data Source = "+ path +"; persist Security info = false "); // creates a connection object and a connection string.

Con. open (); // connect to the database

Add:
String SQL = "insert into T1 (name, PWD) values (@ A, @ B )";

Oledbparameter p1 = new oledbparameter ("@ A", textbox1.text );
Oledbparameter P2 = new oledbparameter ("@ B", textbox2.text );
Oledbcommand COM = new oledbcommand (SQL, Ole );

Com. Parameters. Add (P1 );
Com. Parameters. Add (P2 );
Com. executenonquery ();

Delete:

String SQL = "delete from T1 where name = @ ";

Oledbparameter p1 = new oledbparameter ("@ A", textbox1.text );
Oledbcommand COM = new oledbcommand (SQL, Ole );

Com. Parameters. Add (P1 );
Com. executenonquery ();

Change:

String SQL = "Update T1 set Pwd = @ A where name = @ B ";

Oledbparameter p1 = new oledbparameter ("@ A", textbox1.text );
Oledbparameter P2 = new oledbparameter ("@ B", textbox2.text );
Oledbcommand COM = new oledbcommand (SQL, Ole );

Com. Parameters. Add (P1 );
Com. Parameters. Add (P2 );
Com. executenonquery ();

Query:

String SQL = "select * From T2 where name = @ A and Pwd = @ B ";

Oledbparameter p1 = new oledbparameter ("@ A", textbox1.text );
Oledbparameter P2 = new oledbparameter ("@ B", textbox2.text );
Oledbcommand COM = new oledbcommand (SQL, con );

Com. Parameters. Add (P1 );
Com. Parameters. Add (P2 );

Oledbdatareader DR = com. executereader (); // different from the first three
If (dr. Read ())
{
MessageBox. Show ("Log on! ");
}

 

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.