我建立了一个存储过程:
带了两个输入参数
一个输出参数CREATE PROCEDURE RowsAndOutPut
(
@InPutParam1 int
@InPutParam2 int
@OutPutParam int OUTPUT
)
AS
select @OutPutParam =count(*) from customers where customersid=@InPutParam
.........
........
GO
我看了一些资料的写法,比如:
/// <summary>
/// 这个方法用于绑定命令对象与参数.
///
/// 这个方法给每个输入参数、输入输出参数(输出参数、返回值参数除外)的值为 null 的参数分配值为 DBNull
/// </summary>
/// <param name="command">需要增加参数的命令</param>
/// <param name="commandParameters">参数数组,要增加到命令的参数</param>
private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)
{
if( command == null ) throw new ArgumentNullException( "command" );
if( commandParameters != null )
{
foreach (SqlParameter p in commandParameters)
{
if( p != null )
{
// Check for derived output value with no value assigned
if ( ( p.Direction == ParameterDirection.InputOutput ||
p.Direction == ParameterDirection.Input ) &&
(p.Value == null) || p.Value.ToString() == string .Empty)
{
p.Value = DBNull.Value;
}
command.Parameters.Add(p);
}
}
}
}
==================
#region 执行存储过程Command方法,获得一个命令对象
/// <summary>
/// 执行存储过程Command方法
/// </summary>
/// <param name="SqlName">存储过程名</param>
/// <param name="arrParam">存储过程参数数组</param>
/// <returns></returns>
private SqlCommand ExecuteCommand(string SqlName,params SqlParameter[] arrParam)
{
SqlCommand SqlComm = new SqlCommand(SqlName,FConnection); if(arrParam != null)
{
SqlComm.CommandType = CommandType.StoredProcedure;
foreach(SqlParameter param in arrParam)
{
SqlComm.Parameters.Add(param);
}
} return SqlComm;
}
#endregion
俺是C#的菜鸟,看了半天,还是不知道怎么把我的存储过程带进去执行,请指点.
带了两个输入参数
一个输出参数CREATE PROCEDURE RowsAndOutPut
(
@InPutParam1 int
@InPutParam2 int
@OutPutParam int OUTPUT
)
AS
select @OutPutParam =count(*) from customers where customersid=@InPutParam
.........
........
GO
我看了一些资料的写法,比如:
/// <summary>
/// 这个方法用于绑定命令对象与参数.
///
/// 这个方法给每个输入参数、输入输出参数(输出参数、返回值参数除外)的值为 null 的参数分配值为 DBNull
/// </summary>
/// <param name="command">需要增加参数的命令</param>
/// <param name="commandParameters">参数数组,要增加到命令的参数</param>
private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)
{
if( command == null ) throw new ArgumentNullException( "command" );
if( commandParameters != null )
{
foreach (SqlParameter p in commandParameters)
{
if( p != null )
{
// Check for derived output value with no value assigned
if ( ( p.Direction == ParameterDirection.InputOutput ||
p.Direction == ParameterDirection.Input ) &&
(p.Value == null) || p.Value.ToString() == string .Empty)
{
p.Value = DBNull.Value;
}
command.Parameters.Add(p);
}
}
}
}
==================
#region 执行存储过程Command方法,获得一个命令对象
/// <summary>
/// 执行存储过程Command方法
/// </summary>
/// <param name="SqlName">存储过程名</param>
/// <param name="arrParam">存储过程参数数组</param>
/// <returns></returns>
private SqlCommand ExecuteCommand(string SqlName,params SqlParameter[] arrParam)
{
SqlCommand SqlComm = new SqlCommand(SqlName,FConnection); if(arrParam != null)
{
SqlComm.CommandType = CommandType.StoredProcedure;
foreach(SqlParameter param in arrParam)
{
SqlComm.Parameters.Add(param);
}
} return SqlComm;
}
#endregion
俺是C#的菜鸟,看了半天,还是不知道怎么把我的存储过程带进去执行,请指点.
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "RowsAndOutPut";SqlParameter param = new SqlParameter("@InPutParam1", SqlDbType.Int);
param.Value = 值1;
cmd.Parameters.Add(param);param = new SqlParameter("@InPutParam2", SqlDbType.Int);
param.Value = 值2;
cmd.Parameters.Add(param);param = new SqlParameter("@OutPutParam", SqlDbType.Int);
param.Direction = ParameterDirection.Output;
cmd.Parameters.Add(param);cmd.ExecuteNonQuery();int value = int.Parse(cmd.Parameters["@OutPutParam"].Value); //这就是你要的.
{
string sql="tp_Fetch_List";
System.Data.SqlClient.SqlParameter[] p=new SqlParameter[5];
p[0]=new SqlParameter();
p[0].ParameterName ="@page_num";
p[0].Value =page_num;
p[0].DbType=System.Data.DbType.Int32; p[1]=new SqlParameter();
p[1].ParameterName ="@row_in_page";
p[1].Value =row_in_page;
p[1].DbType=System.Data.DbType.Int32; p[2]=new SqlParameter();
p[2].ParameterName ="@order_column";
p[2].Value =order_column;
p[2].DbType=System.Data.DbType.String; p[3]=new SqlParameter();
p[3].ParameterName ="@row_total";
p[3].Direction=System.Data.ParameterDirection.Output;
p[3].DbType=System.Data.DbType.Int32; p[4]=new SqlParameter();
p[4].ParameterName ="@comb_condition";
p[4].Value =comb_condition;
p[4].DbType=System.Data.DbType.String;
DataSet ds=ExecSPDataSet(sql,p);
if (p[3].Value!=DBNull.Value && p[3].Value.ToString()!=string.Empty )
output=Convert.ToInt32(p[3].Value);
return ds;
}
public static DataSet ExecSPDataSet(string sql,System.Data.IDataParameter[] paramers)
{
SqlConnection conn=new SqlConnection(ConnectionString);
SqlCommand sqlcom=new SqlCommand(sql,conn);
sqlcom.CommandType= CommandType.StoredProcedure ; foreach(System.Data.IDataParameter paramer in paramers)
{
sqlcom.Parameters.Add(paramer);
}
conn.Open();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=sqlcom;
DataSet ds=new DataSet();
da.Fill(ds);
conn.Close();
return ds;
}