Ado. Net-command object

Source: Internet
Author: User

We already know how to connect to the database. After opening the database, how can we operate the database? This requires the use of the command object, commandObjects can be added, deleted, modified, and queried on the database.

Like the connection object, the command object belongs to the. NET Framework dataProgramDifferent data providers have their own command objects. HereWe also discuss the command object based on SQL Server.

Main attributes and methods of the command object

Attribute

ConnectionDatabase connection used by the command object

CommandtextExecuted SQL statement

Method

Executenonquery ()Execute a statement that does not return rows, such as an update statement.

Executereader ()Run the query command to return the datareader object.

Executescalar ()Returns a single value, for example, execute count (*)

Here, we will focus on the executescalar () method of the command object. This method only returns the value of the first column in the first row of the query result.This method is usually used when the query result has only one value, for example, when an aggregate function is used. The Return Value of the method must be explicitly converted. BecauseThe returned value belongs to the object type.

To use the command object, you must have an available connection object. The steps for using the command object include:

1. Create a database connection

Create a connection object.

2. Define the executed SQL statement.

In general, the SQL statement to be executed is assigned to a string.

3. Create a command object

Use an existing connection object and SQL statement string to create a command object.

4. Execute SQL statements

Execute a command using a method of the command object.

Example:

Code:
  1. // The automatically generated reference namespace is omitted.
  2. UsingSystem. Data. sqlclient;
  3. NamespaceOpenclosedb
  4. {
  5. ClassDemo
  6. {
  7. Static VoidMain ()
  8. {
  9. StringConnstring ="Data Source =.; initial catalog = myschool; user id = sa; Pwd = 123456";
  10. Sqlconnection connection =NewSqlconnection (connstring );
  11. StringSQL =String. Format ("Select count (*) from student");
  12. Sqlcommand objcommand =NewSqlcommand (SQL, connection );
  13. Connection. open ();
  14. IntNumber = (Int) Objcommand. executescalar ();
  15. Connection. Close ();
  16. Console. writeline ("A total of {0} records", Number );
  17. }
  18. }
  19. }

Note: you must open the database before using the command object to execute 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.