在.NET中要访问MySql,我采用的是MySql官方提供的.NET connector,此驱动只需要下载即可使用:http://dev.mysql.com/downloads/connector/net/1.0.html
在项目中加载 using MySql.Data.MySqlClient;
存储过程如下:
CREATE PROCEDURE `test`(in firstparam int, in secondparam int, out outerparam int)
BEGIN
set outerparam = firstparam * secondparam;
END
调用程序如下:
MySqlConnection mySqlcon = new MySqlConnection("server=localhost;database=xxxx;user id=xxxx;pwd=xxxx");
MySqlCommand myCmd = new MySqlCommand("test",mySqlcon);
myCmd.CommandType = CommandType.StoredProcedure;
myCmd.Parameters.Add("?firstparam",MySqlDbType.Int32);
myCmd.Parameters[0].Value = 13;
myCmd.Parameters.Add("?secondparam",MySqlDbType.Int32);
myCmd.Parameters[1].Value = 11;
myCmd.Parameters.Add("?outerparam",MySqlDbType.Int32);
myCmd.Parameters[2].Direction = ParameterDirection.Output;
mySqlcon.Open();
myCmd.ExecuteNonQuery();
string s = myCmd.Parameters[2].Value.ToString();
mySqlcon.Close();
Response.Write(s);