Databases Reference
In-Depth Information
// Set the Connection property of the Command object
DBCmd.Connection = DBConn;
// Set the text of the Command to the INSERT statement
DBCmd.CommandText = "INSERT into employees" +
"VALUES (15, 'HAYES', 'ADMIN', 6, " +
"'17-APR-2002', 18000, NULL, 4)";
// Set the transaction property of the Command object
DBCmd.Transaction = DBTxn;
// Execute using ExecuteNonQuery because we do not
// retrieve a result set
DBCmd.ExecuteNonQuery();
// Commit the transaction
DBTxn.Commit();
// Close the connection
DBConn.Close();
Performance Tip
If your SQL statement retrieves a single value, such as a sum or count,
execute that statement using the ExecuteScalar method of the Command
object. Again, you can use the ExecuteReader method to execute state-
ments that retrieve a single value, but using the ExecuteScalar method
allows the data provider to optimize for a result set that consists of a sin-
gle row and column. The data provider improves performance by avoid-
ing much of the same overhead described previously for ExecuteReader
versus ExecuteNonQuery .
The following example shows how to retrieve the count of all employees
with a yearly salary of more than $50,000 from the employees table using
ExecuteScalar:
// Open a connection to the database
SybaseConnection Conn;
Search WWH ::




Custom Search