I tried several ways to insert data to the database.

Source: Internet
Author: User
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!

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.