SQLcommand represents a T-SQL statement or stored procedure to be run against the SQL database. to run a large number of operations or work with the database structure.
This is often used when visiting the database. Let's see how it's done!
First, the attribute
CommandText |
Gets or sets the data source to run on the Transact - SQL statement or stored procedure. |
CommandType |
Gets or sets a value that indicates how to interpret the CommandText property. CommandType.Text representative is running the SQL Statement commandtype.storeprocedure represents a stored procedure that is running CommandType.TableDirect name of the table to be interviewed |
Connection |
Gets or sets SqlCommand the instance used by the SqlConnection . |
CommandTimeout |
Gets or sets the wait time before terminating the attempt to run the command and generating an error. time to wait for the command to run ( in seconds ) . The default value is zero seconds. |
Second, the method
ExecuteNonQuery () |
int |
For example: Pre name= "code" class= "VB" >public Create (Myexecutequery as string,myconnectionstring as String) Dim conn as New SqlConnect Ion ' definition Connection database Dim cmd As New SqlCommand ' define DATABASE command conn = New SqlConnection (sqlutil.connstring) ' Instantiate the string returned in Sqlutil cmd.connection = conn Conn.Open () ' Open Database Cmd.executenonquery () ' Returns the affected Line Conn.close () ' Close database |
. ExecuteReader () |
It has a return type of SqlDataReader . This method is used for query operations performed by the user. Read () using the SqlDataReader object ; method to perform line-wise |
Like what: SqlCommand Comm =new SqlCommand ("select * from Cgsz where cid=" +id,conn); SqlDataReader Reder=comm. ExecuteReader (); while (Reder. Read ()) {//reads the content column string str=reder["CNAME"]. ToString ();//Read the categorical column string str1=reder["Ckind"]. ToString ();//text box loading Data This.txtContent.Text = Str;this.txtclass.text = str1;}
the time to read the data column. In addition to using the reder[" column name "]. ToString (); can also use reder[ index ]. Tosting ();< Note: The index here refers to the index of the column in the database. Start from 0 . > |
ExecuteScalar () |
Its return value type is more than int select count sum |
Like what: Public Create (Myexecutequery as String. myConnectionString As String) Dim cmd as New SqlCommand ' Define Database command Conn.Open () ' Open Database Cmd.executenonquery () ' Returns the first column of the first row in the result set or a null reference (assuming that the result set is empty). Conn.close () ' Close database |
The accumulation of little drops
Accumulate--sqlcommand Commands