Executenonquery,executereader,executescalar differences

Source: Internet
Author: User

ExecuteNonQuery Method: Perform non-query SQL operations, including INSERT, delete, change update
Excutereader method: Execute Query, return DataReader, dr["column name" via DataReader object)
You can get data, read one line at a time, and pass the while (Dr. Read ()) for cyclic reading
ExecuteScalar method: Executes the query, returning only one data

When you connect to a SQL Server database: First create the SqlConnection class and the SqlCommand class instance to connect to the SQL Server database and execute the SQL statement commands, respectively. Then open the data connection and execute the SQL statement using the appropriate method of SqlCommand.
The ExecuteNonQuery () method executes the SQL statement and does not return data;
The ExecuteReader () method sends an SQL statement to SqlConnection and produces a SqlDataReader class object that contains the data returned by the SQL command;
The ExecuteScalar () method executes the SQL query and returns the first column of the first row in the query result set, ignoring the extra columns or rows!

For example:
SqlConnection myconncetion = new SqlConnection (m_sqlconnection);
SqlCommand mycmd = new SqlCommand (P_strsql, myconncetion);
One:
Myconncetion.open ();
Mycmd.executenonquery ();
Two:
Myconncetion.open ();
SqlDataReader myreader = Mycmd.executereader ();
if (Myreader.read ())
{
return 0;
}
Else
{
throw new Exception ("Value unavailable!");
}
Three:
Myconnection.open ();
SqlDataAdapter SqlDa = new SqlDataAdapter (P_strsql, MyConnection);
DataSet ds = new DataSet ("DS");
Sqlda.fill (Ds);

Four:


Myconncetion.open ();
Object r = Mycmd.executescalar ();
if (object. Equals (R, NULL))
{
throw new Exception ("Value unavailable!");

}
Else
{
return (int) r;

}

Note: the ExecuteReader () method needs to be used with the SqlDataReader object, the resulting data set is read-only and the cursor can only be moved from front to back.

String OSQL = "SELECT id, password, name, level from verify"; SqlCommand comm = new SqlCommand (oSql, con); con. Open ();//The database connection is opened before the method is called, which reduces the time spent on database connections and saves database resources. SqlDataReader dr = Comm. ExecuteReader (); while (Dr.    Read ()) {String id = ""; id = dr[0]. ToString (); (0 is the first column)//or id=dr["id"]. ToString (); (reference field name)//or Id= Dr. GetString (Dr. GetOrdinal ("id"));} The database cannot be closed until the data is read, because the data source of the SqlDataReader object must maintain a database connection. Con. Close ();

This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/wuyujie1219/archive/2009/07/26/4380956.aspx

Executenonquery,executereader,executescalar differences

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.