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.