.net下mysql存储过程返回自定义值(原创)
本人开发的开发者技术变现资源聚集地,大家支持下,下面是网址
https://www.baiydu.com
ado.net方法
public static string ExecuteQueryProc(string sConnStr, string sSql, Hashtable args) { MySqlCommand cmd = null; try { cmd = new MySqlCommand(sSql); cmd.Connection = new MySqlConnection(sConnStr); cmd.CommandType = CommandType.StoredProcedure; cmd.Connection.Open(); MySqlParameter mypara = cmd.Parameters.AddWithValue("@ReValue", SqlDbType.NVarChar); mypara.Direction = ParameterDirection.ReturnValue; // cmd.Parameters.AddWithValue("@ReValue", ""); foreach (DictionaryEntry de in args) { if (de.Value.ToString() != null && de.Value.ToString() != "") { cmd.Parameters.AddWithValue("@" + de.Key.ToString(), de.Value.ToString()); } } cmd.ExecuteScalar(); string reValue = (string)cmd.Parameters["@ReValue"].Value.ToString(); return reValue; } finally { cmd.Connection.Close(); if (cmd != null) cmd.Dispose(); } }
存储过程
CREATE PROCEDURE customReturnValue(parName varchar(50),out ReValue varchar(50)
begin
declare reuturnName varchar(50);
SELECT name INTO reuturnName
FROM infosheet WHERE name = parName ;
SELECT reuturnName;
if reuturnName is null
then
insert into infosheet(name)
values (parName);
set ReValue =6;
else
set ReValue =5;
end if;
end
.net调用
Hashtable hasTable = new Hashtable(); hasTable.Add("parName", "小明"); string returnValue = MySQLHelper.ExecuteQueryProc(MySQLHelper.connectionString, "secondProc", hasTable);