Ado. NET QuickStart-Returns a data value using the ExecuteScalar () method of the Command object

Source: Internet
Author: User

Related knowledge:

    1. Some SQL operations, such as SUM, return only one data value from the database, not multiple rows of data
    2. Although you can also use ExecuteReader () to return a DataReader object that represents that data value, it is more convenient to use the ExecuteScalar method of the Command object
    3. ExecuteScalar () Method: The method can only execute SELECT statements and is typically used for statistics, such as returning the number of records that match a condition

code example:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingSystem.Data;7 usingSystem.Data.SqlClient;8 9 namespaceConsoleApplication3Ten { One     class Program A     { -         Static voidMain (string[] args) -         { the             stringstrconn =@"Server=joe-pc;database=adventureworks_wroxssrs2012;uid=sa;pwd=root"; -SqlConnection conn =NewSqlConnection (strconn); -  -             stringStrcmd ="SELECT COUNT (*) from Production.productcategory"; +SqlCommand cmd =NewSqlCommand (STRCMD, conn); -  + Conn. Open (); A  at             intCount = Convert.ToInt32 (cmd. ExecuteScalar ());//ExecuteScalar Returns an object type -  -Console.WriteLine ("there are {0} rows of data. ", count); -  - Conn. Close (); -         } in     } -}

Ado. NET QuickStart-Returns a data value using the ExecuteScalar () method of the Command object

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.