The sqlcommand object has several operation methods.

Source: Internet
Author: User

1. sqlcommand. executenonquery () This method is mainly used to execute SQL statement insert, modify, and delete commands, and return the affected number of rows without returning data from the Operation database.

2. sqlcommand. executereader () This method is mainly used to execute the SELECT statement of SQL. The executereader () method mainly provides the method to read data in the database sequentially, and then returns the sqlreader object, the editor can use the read method to read the content of each field in each record cyclically. To create a sqldatareader, you must call the executereader () method of the sqlcommand object to return data from the data table in the database:

Sqlconnection conn = new sqlconnection ("");
String sqlcmd = "select * from table name ";
Sqlcommand cmd = new sqlcommand (sqlcmd, Conn );
Conn. open ();
Sqldatareader DR = cmd. executereader ();
If (dr. Read () = true)
{
Listbox1.items. Add (string. Format ("[{0}], \" {1} \ "", Dr [0], Dr [1]);
}

Conn. Close ();

3. sqlcommand. excutescaler () is used to return a value. For example, the count () function is used to calculate the number of all records in the table, or sum () is used to calculate the sum of the Data functions. Sqlcommand. commandtext is used to obtain or set to execute T--SQL statements, table names, and stored procedures.

# Region uses sqlcommand. excutescaler () to return the number of rows of data
Sqlconnection conn = new sqlconnection ("");
Sqlcommand cmd = new sqlcommand ();
Cmd. Connection = conn;
Conn. open ();
Cmd. commandtext = "select count (*) from table name ";
Int num = (INT) cmd. executescalar ();
Response. Write (string. Format ("{0}", num ));
# Endregion

 

 

 

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.