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.