10 The Sybase Data Provider : Using Stored Procedures

Using Stored Procedures
The following sections describe how the Sybase data provider supports specific Sybase features for stored procedures. See “Using Stored Procedures” for more information on how the Sybase data provider uses stored procedures.
Using Stored Procedures in a Batch
Batch support for the DataAdapter was released as an integrated component of the ADO.NET 2.0 API, eliminating the need to update each row, one at a time, for each Update call. You can set the UpdateBatchSize property to control the size of each batch update (see “SybaseParameter Class” for more information.
If a stored procedure call is part of the batch and the BindByName connection string option is set to Native Provider Mode, the Sybase data provider issues each stored procedure call as a single command. This avoids possible naming collisions with named parameters, if the parameter names repeat.
Using Named Parameters
Using named parameters in stored procedures lets you bind parameters by name. This means that you do not have to enter the parameters in order.
When using named parameters in stored procedures with the Sybase data provider, you do not use the native style parameter marker (@) in your SQL statement. Instead, you use ? as the marker.
For example, suppose you have a stored procedure with the following parameters:
CREATE PROCEDURE MYPROC (p0 IN INTEGER, p1 IN CHAR, p3 IN CHAR
DEFAULT 'ADO.NET')
When the BindByName property is set to True (see “SybaseParameter Class”), you can bind the parameters out of order, as shown in the following C# code fragment:
cmd.BindByName = True; // Required to enable this feature
cmd.Parameters.Add(new SybaseParameter("p1", "DataDirect"));
cmd.Parameters.Add(new SybaseParameter("p0", 8888));
cmd.Parameters.Add(new SybaseParameter("p3", "Sybase"));
In this case, the parameter names match the names in the stored procedure definition.
The BindByName property also allows you to use a parameter’s default value. For example, in the procedure definition, the p3 parameter has a default value of ADO.NET. To use that default value, simply omit that parameter from the binding:
cmd.CommandText = "{call MYPROC (?, ?)}";
// Only two ?s used
In this case, you would bind only two parameters on the command:
cmd.BindByName = True; // Required to enable this feature
cmd.Parameters.Add(new SybaseParameter("p0", 8888));
cmd.Parameters.Add(new SybaseParameter("p1", "DataDirect"));
You can also use CommandType.StoredProcedure to execute the stored procedure:
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "MYPROC";
cmd.BindByName = True;
cmd.Parameters.Add(new SybaseParameter("p1", "DataDirect"));
cmd.Parameters.Add(new SybaseParameter("p0", 8888));