Oracle 5.0 Reference Manual page 1884

Table of Contents

Advertisement

DELIMITER //
CREATE PROCEDURE country_hos
(IN con CHAR(20))
BEGIN
SELECT Name, HeadOfState FROM Country
WHERE Continent = con;
END //
DELIMITER ;
Test that the stored procedure works as expected by typing the following into the
interpreter:
CALL country_hos('Europe');
Note that The stored routine takes a single parameter, which is the continent to restrict your search to.
Having confirmed that the stored procedure is present and correct, you can see how to access it from
Connector/Net.
Calling a stored procedure from your Connector/Net application is similar to techniques you have seen
earlier in this tutorial. A
parameter, it takes the name of the stored procedure to call. Set the
of stored procedure, as shown by the following code snippet:
string rtn = "country_hos";
MySqlCommand cmd = new MySqlCommand(rtn, conn);
cmd.CommandType = CommandType.StoredProcedure;
In this case, the stored procedure requires you to pass a parameter. This can be achieved using the
techniques seen in the previous section on parameters,
as shown in the following code snippet:
cmd.Parameters.AddWithValue("@con", "Europe");
The value of the parameter
simplicity it is set as a static string in this example.
At this point, everything is set up and you can call the routine using techniques also learned in earlier
sections. In this case, the
Complete working code for the stored procedure example is shown below:
using System;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
public class Tutorial6
{
public static void Main()
{
string connStr = "server=localhost;user=root;database=world;port=3306;password=******;";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
string rtn = "country_hos";
MySqlCommand cmd = new MySqlCommand(rtn, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@con", "Europe");
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr[0] + " --- " + rdr[1]);
}
Connector/Net Tutorials
object is created, but rather than taking an SQL query as a
MySqlCommand
could more realistically have come from a user input control, but for
@con
method of the
ExecuteReader
1864
MySqlCommand
Section 20.2.4.1.4, "Working with
object is used.
MySqlCommand
command
mysql
object to the type
Parameters",

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents