1、在数据库创建存储过程
CREATE PROCEDURE getNewsTitl(IN newid INT,OUT newTitle VARCHAR(32))
BEGIN
SELECT title INTO newTitle from news WHERE id=newid;
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 outputparameter
{
class Program
{
static void Main(string[] args)
{
//输出参数的使用
MySqlConnection connection = null;
connection = new MySqlConnection();
string str = ConfigurationManager.ConnectionStrings["conStri"].ConnectionString;
connection.ConnectionString = str;
MySqlCommand sqlCommand = new MySqlCommand("getNewsTitl",connection);
sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
connection.Open();
//输入参数
MySqlParameter p1 = new MySqlParameter();
p1.ParameterName = "@newid";
p1.Value = 1;
//输出参数
MySqlParameter p2 = new MySqlParameter();
p2.ParameterName = "@newTitle";
p2.MySqlDbType = MySqlDbType.VarChar;
p2.Size = 32;
p2.Direction = System.Data.ParameterDirection.Output;
sqlCommand.Parameters.Add(p1);
sqlCommand.Parameters.Add(p2);
sqlCommand.ExecuteScalar();
connection.Close();
Console.WriteLine(p2.Value);
connection.Close();
Console.ReadKey();
}
}
}
3、点击运行