Connector/Net Programming
1909
Note
When you call a stored procedure, the command object makes an additional
SELECT
call to determine the parameters of the stored procedure. You must
ensure that the user calling the procedure has the
SELECT
[578]
privilege on the
mysql.proc
table to enable them to verify the parameters. Failure to do this
will result in an error when calling the procedure.
This section will not provide in-depth information on creating Stored Procedures. For such information,
please refer to
http://dev.mysql.com/doc/mysql/en/stored-routines.html
.
A sample application demonstrating how to use stored procedures with Connector/Net can be found in
the
Samples
directory of your Connector/Net installation.
20.2.5.9.1. Using Stored Routines from Connector/Net
Stored procedures in MySQL can be created using a variety of tools. First, stored procedures can
be created using the
mysql
command-line client. Second, stored procedures can be created using
MySQL Workbench. Finally, stored procedures can be created using the
.ExecuteNonQuery
method
of the
MySqlCommand
object.
Unlike the command-line and GUI clients, you are not required to specify a special delimiter when
creating stored procedures in Connector/Net.
To call a stored procedure using Connector/Net, you create a
MySqlCommand
object and pass the
stored procedure name as the
.CommandText
property. You then set the
.CommandType
property to
CommandType.StoredProcedure
.
After the stored procedure is named, you create one
MySqlCommand
parameter for every parameter
in the stored procedure.
IN
parameters are defined with the parameter name and the object containing
the value,
OUT
parameters are defined with the parameter name and the data type that is expected to
be returned. All parameters need the parameter direction defined.
After defining the parameters, you call the stored procedure by using the
MySqlCommand.ExecuteNonQuery()
method.
Once the stored procedure is called, the values of the output parameters can be retrieved by using the
.Value
property of the
MySqlConnector.Parameters
collection.
Note
When a stored procedure is called using
MySqlCommand.ExecuteReader
,
and the stored procedure has output parameters, the output parameters are
only set after the
MySqlDataReader
returned by
ExecuteReader
is closed.
The following C# example code demonstrates the use of stored procedures. It assumes the database
'employees' has already been created:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace UsingStoredRoutines
{
class Program
{
static void Main(string[] args)
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...