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! ");
}