SQL Server Returns a field

Source: Internet
Author: User

Visual C # tutorial on Dynamically Operating SQL Server database instances (6): Method for returning a field

The preceding describes how to execute SQL statements with parameters and stored procedures with parameters.Method for returning a fieldIt calls the SqlHelper class to execute the SqlHelper. ExecuteReader () method. The example is;

Object myobj = SqlHelper. ExecuteScalar (conn, CommandType. Text, SQL, null );

The four parameters are passed:

"Conn"-is a link character;

"CommandType. Text"-indicates the type of the SQL command to be executed;

"SQL"-the SQL command statement to be executed;

"Null"-the fourth parameter is null.

Protected void btnExecuteScalar_Click (object sender, EventArgs e)
{
// Obtain the command to be executed
String SQL = "select top 2 title from menularticle where classID_A = 8 ";
SqlCommand cmd = new SqlCommand ();
// Defines the storage range of object Resources. Once the using range ends, the resources occupied by the other party are released.
Using (SqlConnection conn = new SqlConnection (SqlHelper. ConnectionStringLocalTransaction ))
{
// Open the connection
Conn. Open ();
// Call the execution method. Because there is no parameter, the last item is directly set to null.
// Note: The returned result is of the object type.
Object myobj = SqlHelper. ExecuteScalar (conn, CommandType. Text, SQL, null );
// Display the returned results
Response. Write ("");
}
}

For more information, see my website.

Related Article

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.