Use sqlcommand object in Asp.net

Source: Internet
Author: User

 

1. Common sqlcommand attributes: sqlcommand attributes are used to prepare for executing commands.

1. commandtext attributes: executed SQL statements; 2. Connection attributes: Connection database sqlconnection objects; 3. commandtype attributes: parses commandtext values; sqlcommand cmd = new sqlcommand ("login ", conn); cmd. commandtype = commandtype. storedprocedure; // The stored procedure is used here. 4. commandtimeout attribute: Set the duration of execution to stop. 5. Parameters attribute: set parameters; ii. sqlcommand class constructor sqlcommand mycommand = new sqlcommand (sqlupdate, Conn); III. Common sqlcommand Methods: sqlcommand mainly executes SQL statements. 1. executereader () method: Mainly executes select statements. Return the result Sqldatareader object. For example, sqlcommand myconn = new sqlcommand ("select * From v_economical my2_comidd where eid =" + request. querystring ["Eid"] + "", Conn); Conn. open (); sqldatareader RD = myconn. executereader (); Rd. read (); lbyear1.text = RD ["year1"]. tostring (); lbmonth1.text = RD ["month1"]. tostring (); lbcom_name.text = RD ["com_name"]. tostring (); Rd. close (); Conn. close (); 2. executenonquery () method: Mainly executes insert, update, and delete statements. The returned value is the number of rows affected by the command. Example: protected void button#click (Object sender, eventargs e) {string class_name = textbox1.text; string Pwd = pwdmd5.md5l ("111 "); sqlcommand myconn = new sqlcommand ("insert into useradmin (username, userpwd, userlevel, Tim, num) values ('" + class_name + "', '" + PWD + "', 'U', @ Tim, 1) ", Conn); myconn. parameters. add (New sqlparameter ("@ Tim", sqldbtype. datetime, 8); myconn. parameters ["@ Tim"]. value = datetime. Now. tostring (); // display the detailed Date and Time Conn. open (); myconn. executenonquery (); Conn. close (); response. write ("<script language = 'javascript '> alert (' added the Administrator successfully! The initial password is 123456 '); location = 'adusermag. aspx '</SCRIPT> ");} protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs e) {string sqlupdate =" Update useradmin set userpwd = @ userpwd where userid =' "+ Int. parse (gridview1.datakeys [E. rowindex]. value. tostring (). trim () + "'"; sqlcommand myconn = new sqlcommand (sqlupdate, Conn); myconn. parameters. add (New sqlparameter ("@ userpwd", sqldbtype. varc Har, 500); myconn. parameters ["@ userpwd"]. value = pwdmd5.md5l ("11111 eaderonquery"); Conn. open (); myconn. executenonquery (); Conn. close (); response. write ("<script language = 'javascript '> alert ('password restored !! Reset Password to 111 '); location = 'adusermag. aspx '</SCRIPT> ");} protected void gridview1_rowdeleting (Object sender, gridviewdeleteeventargs e) {string sqldel =" delete from useradmin where userid = "+ Int. parse (gridview1.datakeys [E. rowindex]. value. tostring (). trim (); sqlcommand myconn = new sqlcommand (sqldel, Conn); Conn. open (); myconn. executenonquery (); // lbsql. TEXT = "<B> deleted records </B> <br>" + sqldel; Conn. close (); Bindgrid () ;}3. executescalar () method: return the obtained aggregate value (total number of rows of data ). 4. executexmlreader () method.

 

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.