SQLcommand represents a T-SQL statement or stored procedure to be executed against the SQL database. to perform a large number of operations or work with the database structure.
This is often used when accessing a database. Let's see how it's done!
First, the attribute
CommandText |
Gets or sets the data source to be executed on the Transact - SQL statement or stored procedure. |
CommandType |
Gets or sets a value that indicates how to interpret the CommandText property. CommandType.Text The representative is doing SQL Statement commandtype.storeprocedure represents the execution of a stored procedure CommandType.TableDirect The name of the table to access |
connection |
Gets or sets the Span style= "font-family:arial" >sqlcommand sqlconnection |
CommandTimeout |
Gets or sets the wait time before terminating an attempt to execute a command and generating an error. time to wait for the command to execute ( in seconds ) . The default value is zero seconds. |
Second, the method
ExecuteNonQuery () |
It has a return value type of int type. Many are used to perform additions, deletions, and modifications to data. Returns the number of rows affected. |
For example: public Create (Myexecutequery as string,myconnectionstring as String) Dim conn As New SqlConnection ' Define Connection database Dim cmd As New SqlCommand ' define DATABASE command conn = New SqlConnection (sqlutil.connstring) ' instantiates the string returned in Sqlutil Nection = conn Conn.Open () ' Open Database Cmd.executenonquery () ' Returns the affected row Conn.close () ' Off 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 |
For example: 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 ();//Load data for text box This.txtContent.Text = Str;this.txtclass.text = str1;}
The time in which the data column is read. In addition to using the reder[" column name "]. ToString (); You can also use reder[ index ]. Tosting ();< Note: The index here refers to the index of the column in the database. Starting from 0 . > |
ExecuteScalar () |
Its return value type is more than int select query. The resulting return result is a value, such as using count sum |
For example: Public Create (Myexecutequery as string,myconnectionstring as String) Dim cmd as New SqlCommand ' Define Database command Conn.Open () c1/> ' Open Database Cmd.executenonquery () ' Returns the first column of the first row in the result set or a null reference if the result set is empty. Conn.close () ' Close database |
The accumulation of little drops
Accumulate--sqlcommand Commands