1、创建mysql函数
CREATE DEFINER=`root`@`localhost` FUNCTION `getContentByID`(newsid int) RETURNS varchar(255) CHARSET utf8
BEGIN
DECLARE a VARCHAR(255);
SELECT content INTO a from news WHERE id=newsid;
RETURN a;
END
创建成功
2、编写代码
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04returnparameter
{
class Program
{
static void Main(string[] args)
{
//返回参数的使用
MySqlConnection connection = null;
connection = new MySqlConnection();
string str = ConfigurationManager.ConnectionStrings["conStri"].ConnectionString;
connection.ConnectionString = str;
MySqlCommand command = new MySqlCommand("getContentByID", connection);
command.CommandType = System.Data.CommandType.StoredProcedure;
MySqlParameter[] mySqlParameters =
{
new MySqlParameter("newsid",1),
new MySqlParameter("a",MySqlDbType.VarChar,255)
};
mySqlParameters[1].Direction = System.Data.ParameterDirection.ReturnValue;
command.Parameters.AddRange(mySqlParameters);
connection.Open();
command.ExecuteScalar();
connection.Close();
Console.WriteLine(mySqlParameters[1].Value);
Console.ReadKey();
}
}
}