Connector/Net Tutorials
1863
As well as marking the position of the parameter in the query string, it is necessary to add a parameter
to the Command object. This is illustrated by the following code snippet:
cmd.Parameters.AddWithValue("@Continent", "North America");
In this example the string "North America" is supplied as the parameter value statically, but in a more
practical example it would come from a user input control.
A further example illustrates the complete process:
using System;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
public class Tutorial5
{
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 sql = "SELECT Name, HeadOfState FROM Country WHERE Continent=@Continent";
MySqlCommand cmd = new MySqlCommand(sql, conn);
Console.WriteLine("Enter a continent e.g. 'North America', 'Europe': ");
string user_input = Console.ReadLine();
cmd.Parameters.AddWithValue("@Continent", user_input);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr["Name"]+" --- "+rdr["HeadOfState"]);
}
rdr.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
conn.Close();
Console.WriteLine("Done.");
}
}
In this part of the tutorial you have see how to use parameters to make your code more secure.
20.2.4.1.5. Working with Stored Procedures
This section illustrates how to work with stored procedures. Putting database-intensive operations into
stored procedures lets you define an API for your database application. You can reuse this API across
multiple applications and multiple programming languages. This technique avoids duplicating database
code, saving time and effort when you make updates due to schema changes, tune the performance
of queries, or add new database operations for logging, security, and so on. Before working through
this tutorial, familiarize yourself with the
CREATE PROCEDURE
and
CREATE FUNCTION
statements that
create different kinds of stored routines.
For the purposes of this tutorial, you will create a simple stored procedure to see how it can be called
from Connector/Net. In the MySQL Client program, connect to the World database and enter the
following stored procedure:
Содержание 5.0
Страница 1: ...MySQL 5 0 Reference Manual ...
Страница 18: ...xviii ...
Страница 60: ...40 ...
Страница 396: ...376 ...
Страница 578: ...558 ...
Страница 636: ...616 ...
Страница 844: ...824 ...
Страница 1234: ...1214 ...
Страница 1426: ...MySQL Proxy Scripting 1406 The following diagram shows an overview of the classes exposed by MySQL Proxy ...
Страница 1427: ...MySQL Proxy Scripting 1407 ...
Страница 1734: ...1714 ...
Страница 1752: ...1732 ...
Страница 1783: ...Configuring Connector ODBC 1763 ...
Страница 1793: ...Connector ODBC Examples 1773 ...
Страница 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Страница 1842: ...Connector Net Installation 1822 5 Once the installation has been completed click Finish to exit the installer ...
Страница 1864: ...Connector Net Visual Studio Integration 1844 Figure 20 24 Debug Stepping Figure 20 25 Function Stepping 1 of 2 ...
Страница 2850: ...2830 ...
Страница 2854: ...2834 ...
Страница 2928: ...2908 ...
Страница 3000: ...2980 ...
Страница 3122: ...3102 ...
Страница 3126: ...3106 ...
Страница 3174: ...3154 ...
Страница 3232: ...3212 ...