Well, I just tried several ways to insert data into the database and record it:
Here, the ASPX page has two textbox controls named username and PWD respectively. There is also a submit button, the following Code Are written in the time of the button: (as follows)
1. Use sqlcommand to directly write an SQL statement: String strusername = This . Username. text;
String strpwd = This . Pwd. text;
Sqlconnection Conn = New Sqlconnection ();
Sqlcommand comm = New Sqlcommand ();
Conn = New Sqlconnection ( " Server = localhost; database = demo; uid = sa; Pwd = 123456 " );
Conn. open ();
Comm. Connection = Conn;
Comm. commandtext = " Insert into demo (username, PWD) values (' " + Strusername + " ',' " + Strpwd + " ') " ;
Comm. executenonquery ();
2. Use sqldataadapter and datatable: Sqlconnection Conn = New Sqlconnection ();
Sqlcommand comm = New Sqlcommand ();
Sqldataadapter dataadapter = New Sqldataadapter ();
String strusername = This . Username. text;
String strpwd = This . Pwd. text;
Conn = New Sqlconnection ( " Server = localhost; database = demo; uid = sa; Pwd = 123456 " );
Conn. open ();
Comm = New Sqlcommand ( " Select * from demo " , Conn );
Dataadapter. selectcommand = Comm;
Dataadapter. insertcommand = New Sqlcommandbuilder (dataadapter). getinsertcommand ();
Datatable = New Datatable ();
Dataadapter. Fill (datatable );
Datarow = Datatable. newrow ();
Datarow [ 1 ] = Strusername;
Datarow [ 2 ] = strpwd;
datatable. Rows. Add (datarow);
dataadapter. Update (datatable);
3. Use sqldataadapter and Dataset:Sqlconnection Conn= NewSqlconnection ();
Sqlcommand comm= NewSqlcommand ();
Sqldataadapter dataadapter= NewSqldataadapter ();
String strusername = This . Username. text;
String strpwd = This . Pwd. text;
Conn = New Sqlconnection ( " Server = localhost; database = demo; uid = sa; Pwd = 123456 " );
Conn. open ();
Comm = New Sqlcommand ( " Select * from demo " , Conn );
Dataadapter. selectcommand = Comm;
Dataadapter. insertcommand = New Sqlcommandbuilder (dataadapter). getinsertcommand ();
Dataadapter. Fill (Dataset );
Datarow = Dataset. Tables [ 0 ]. Newrow ();
Datarow [ 1 ] = Strusername;
Datarow [ 2 ] = Strpwd;
Dataset. Tables [ 0 ]. Rows. Add (datarow );
Dataadapter. Update (Dataset );
OK, it's super easy. Oh, it's cainiao ~~~ Please wait until then, a few minutes before please wait until then!