Tải bản đầy đủ - 0 (trang)
9-5. Execute a SQL Command or Stored Procedure

9-5. Execute a SQL Command or Stored Procedure

Tải bản đầy đủ - 0trang

CHAPTER 9 ■ DATABASE ACCESS



Solution

Create a command object appropriate to the type of database you intend to use. All command objects

implement the System.Data.IDbCommand interface. Configure the command object by setting its

CommandType and CommandText properties. Execute the command using the ExecuteNonQuery,

ExecuteReader, or ExecuteScalar method, depending on the type of command and its expected results.



How It Works

The IDbCommand interface represents a database command, and each data provider includes a unique

implementation. Here is the list of IDbCommand implementations for the five standard data providers:





System.Data.Odbc.OdbcCommand







System.Data.OleDb.OleDbCommand







System.Data.OracleClient.OracleCommand







System.Data.SqlServerCe.SqlCeCommand







System.Data.SqlClient.SqlCommand



To execute a command against a database, you must have an open connection (discussed in recipe

9-1) and a properly configured command object appropriate to the type of database you are accessing.

You can create command objects directly using a constructor, but a simpler approach is to use the

CreateCommand factory method of a connection object. The CreateCommand method returns a command

object of the correct type for the data provider and configures it with basic information obtained from

the connection you used to create the command. Before executing the command, you must configure

the properties described in Table 9-3, which are common to all command implementations.

Table 9-3. Common Command Object Properties



Property



Description



CommandText



A string containing the text of the SQL command to execute or the name of a stored

procedure. The content of the CommandText property must be compatible with the

value you specify in the CommandType property.



CommandTimeout



An int that specifies the number of seconds to wait for the command to return before

timing out and raising an exception. Defaults to 30 seconds.



CommandType



A value of the System.Data.CommandType enumeration that specifies the type of

command represented by the command object. For most data providers, valid values

are StoredProcedure, when you want to execute a stored procedure; and Text, when

you want to execute a SQL text command. If you are using the OLE DB data provider,

you can specify TableDirect when you want to return the entire contents of one or

more tables; refer to the .NET Framework SDK documentation for more details.

Defaults to Text.



437



www.it-ebooks.info



CHAPTER 9 ■ DATABASE ACCESS



Property



Description



Connection



An IDbConnection instance that provides the connection to the database on which you

will execute the command. If you create the command using the

IDbConnection.CreateCommand method, this property will be automatically set to the

IDbConnection instance from which you created the command.



Parameters



A System.Data.IDataParameterCollection instance containing the set of parameters to

substitute into the command. (See recipe 9-6 for details on how to use parameters.)



Transaction



A System.Data.IDbTransaction instance representing the transaction into which to

enlist the command. (See the .NET Framework SDK documentation for details about

transactions.)



Once you have configured your command object, you can execute it in a number of ways,

depending on the nature of the command, the type of data returned by the command, and the format in

which you want to process the data.





To execute a command that does not return database data (such as INSERT, DELETE,

or CREATE TABLE), call ExecuteNonQuery. For the UPDATE, INSERT, and DELETE

commands, the ExecuteNonQuery method returns an int that specifies the number

of rows affected by the command. For other commands, such as CREATE TABLE,

ExecuteNonQuery returns the value -1.







To execute a command that returns a result set, such as a SELECT statement or

stored procedure, use the ExecuteReader method. ExecuteReader returns an

IDataReader instance (discussed in recipe 9-7) through which you have access to

the result data. Most data providers also allow you to execute multiple SQL

commands in a single call to the ExecuteReader method, as demonstrated in the

example in recipe 9-7, which also shows how to access each result set.







If you want to execute a query but only need the value from the first column of the

first row of result data, use the ExecuteScalar method. The value is returned as an

object reference that you must cast to the correct type.



■ Note The IDbCommand implementations included in the Oracle and SQL data providers implement additional

command execution methods. Recipe 9-8 describes how to use the ExecuteXmlReader method provided by the

SqlCommand class. Refer to the .NET Framework’s SDK documentation for details on the additional

ExecuteOracleNonQuery and ExecuteOracleScalar methods provided by the OracleCommand class.



The Code

The following example demonstrates the use of command objects to update a database record, run a

stored procedure, and obtain a scalar value:



438



www.it-ebooks.info



CHAPTER 9 ■ DATABASE ACCESS



using System;

using System.Data;

using System.Data.SqlClient;

namespace Apress.VisualCSharpRecipes.Chapter09

{

class Recipe09_05

{

public static void ExecuteNonQueryExample(IDbConnection con)

{

// Create and configure a new command.

IDbCommand com = con.CreateCommand();

com.CommandType = CommandType.Text;

com.CommandText = "UPDATE Employees SET Title = 'Sales Director'" +

" WHERE EmployeeId = '5'";

// Execute the command and process the result.

int result = com.ExecuteNonQuery();

if (result == 1)

{

Console.WriteLine("Employee title updated.");

}

else

{

Console.WriteLine("Employee title not updated.");

}

}

public static void ExecuteReaderExample(IDbConnection con)

{

// Create and configure a new command.

IDbCommand com = con.CreateCommand();

com.CommandType = CommandType.StoredProcedure;

com.CommandText = "Ten Most Expensive Products";

// Execute the command and process the results.

using (IDataReader reader = com.ExecuteReader())

{

Console.WriteLine("Price of the Ten Most Expensive Products.");

while (reader.Read())

{

// Display the product details.

Console.WriteLine(" {0} = {1}",

reader["TenMostExpensiveProducts"],

reader["UnitPrice"]);

}

}

}



439



www.it-ebooks.info



CHAPTER 9 ■ DATABASE ACCESS



public static void ExecuteScalarExample(IDbConnection con)

{

// Create and configure a new command.

IDbCommand com = con.CreateCommand();

com.CommandType = CommandType.Text;

com.CommandText = "SELECT COUNT(*) FROM Employees";

// Execute the command and cast the result.

int result = (int)com.ExecuteScalar();

Console.WriteLine("Employee count = " + result);

}

public static void Main()

{

// Create a new SqlConnection object.

using (SqlConnection con = new SqlConnection())

{

// Configure the SqlConnection object's connection string.

con.ConnectionString = @"Data Source = .\sqlexpress;" +

"Database = Northwind; Integrated Security=SSPI";

// Open the database connection and execute the example

// commands through the connection.

con.Open();

ExecuteNonQueryExample(con);

Console.WriteLine(Environment.NewLine);

ExecuteReaderExample(con);

Console.WriteLine(Environment.NewLine);

ExecuteScalarExample(con);

}

// Wait to continue.

Console.WriteLine(Environment.NewLine);

Console.WriteLine("Main method complete. Press Enter.");

Console.ReadLine();

}

}

}



440



www.it-ebooks.info



CHAPTER 9 ■ DATABASE ACCESS



9-6. Use Parameters in a SQL Command or Stored Procedure

Problem

You need to set the arguments of a stored procedure or use parameters in a SQL command to improve

flexibility.



Solution

Create parameter objects appropriate to the type of command object you intend to execute. All

parameter objects implement the System.Data.IDataParameter interface. Configure the parameter

objects’ data types, values, and directions, and add them to the command object’s parameter collection

using the IDbCommand.Parameters.Add method.



How It Works

All command objects support the use of parameters, so you can do the following:





Set the arguments of stored procedures.







Receive stored procedure return values.







Substitute values into text commands at runtime.



The IDataParameter interface represents a parameter, and each data provider includes a unique

implementation. Here is the list of IDataParameter implementations for the five standard data providers:





System.Data.Odbc.OdbcParameter







System.Data.OleDb.OleDbParameter







System.Data.OracleClient.OracleParameter







System.Data.SqlServerCe.SqlCeParameter







System.Data.SqlClient.SqlParameter



To use parameters with a text command, you must identify where to substitute the parameter’s

value within the command. The ODBC, OLE DB, and SQL Server CE data providers support positional

parameters; the location of each argument is identified by a question mark (?). For example, the

following command identifies two locations to be substituted with parameter values:

UPDATE Employees SET Title = ? WHERE EmployeeId = ?

The SQL Server and Oracle data providers support named parameters, which allow you to identify

each parameter location using a name preceded by the at symbol (@). Here is the equivalent command

using named parameters:

UPDATE Employees SET Title = @title WHERE EmployeeId = @id

To specify the parameter values to substitute into a command, you must create parameter objects of

the correct type and add them to the command object’s parameter collection accessible through the

Parameters property. You can add named parameters in any order, but you must add positional

parameters in the same order they appear in the text command. When you execute your command, the



441



www.it-ebooks.info



CHAPTER 9 ■ DATABASE ACCESS



value of each parameter is substituted into the command string before the command is executed against

the data source. You can create parameter objects in the following ways:





Use the IDbCommand.CreateParameter method.







Use the IDbCommand.Parameters.Add method.







Use System.Data.Common.DbProviderFactory.







Directly create parameter objects using constructors and configure them using

constructor arguments or through setting their properties. (This approach ties you

to a specific database provider.)



A parameter object’s properties describe everything about a parameter that the command object

needs to use the parameter object when executing a command against a data source. Table 9-4 describes

the properties that you will use most frequently when configuring parameters.

Table 9-4. Commonly Used Parameter Properties



Property



Description



DbType



A value of the System.Data.DbType enumeration that specifies the type of data

contained in the parameter. Commonly used values include String, Int32, DateTime,

and Currency.



Direction



A value from the System.Data.ParameterDirection enumeration that indicates the

direction in which the parameter is used to pass data. Valid values are Input,

InputOutput, Output, and ReturnValue.



IsNullable



A bool that indicates whether the parameter accepts null values.



ParameterName



A string containing the name of the parameter.



Value



An object containing the value of the parameter.



When using parameters to execute stored procedures, you must provide parameter objects to satisfy

each argument required by the stored procedure, including both input and output arguments. You must

set the Direction property of each parameter as described in Table 9-4; parameters are Input by default.

If a stored procedure has a return value, the parameter to hold the return value (with a Direction

property equal to ReturnValue) must be the first parameter added to the parameter collection.



The Code

The following example demonstrates the use of parameters in SQL commands. The

ParameterizedCommandExample method demonstrates the use of parameters in a SQL Server UPDATE

statement. The ParameterizedCommandExample method’s arguments include an open SqlConnection and

two strings. The values of the two strings are substituted into the UPDATE command using parameters.

The StoredProcedureExample method demonstrates the use of parameters to call a stored procedure.



442



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

9-5. Execute a SQL Command or Stored Procedure

Tải bản đầy đủ ngay(0 tr)

×