C#获取执行存储过程的"返回值"代码

2/11/2016来源:C#应用人气:2021

以下是C#代码:

/// <summary>
/// 执行存储过程,返回" 返回值"
/// </summary>
/// <param name="storedPRocName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>执行存储过程的返回值</returns>
public static int RunProcedureWithReturn(string storedProcName, IDataParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
int result;
connection.Open();
SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
command.ExecuteNonQuery();
result = (int)command.Parameters["ReturnValue"].Value;
//Connection.Close();
return result;
}
}

/// <summary>
/// 创建 SqlCommand 对象实例(用来返回一个整数值) 
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlCommand 对象实例</returns>
private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
command.Parameters.Add(new SqlParameter("ReturnValue",
SqlDbType.Int, 4, ParameterDirection.ReturnValue,
false, 0, 0, string.Empty, DataRowVersion.Default, null));
return command;
}

 

详细请下载源代码
http://hovertree.com/down/

使用VS2015+SQL 2008


参考:http://hovertree.com/hvtart/bjae/ie4k7lxq.htm

谜语:尘封之后了一生(10笔字)

http://m.hovertree.com/miyu/bjae/8kheebew.htm

 

更多文章:http://www.cnblogs.com/sosoft/p/kaifajishu.html