Accumulate--sqlcommand Commands

Source: Internet
Author: User


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

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.