sqlhelper写调用存储过程方法

    public static object Proc(string ProcName, SqlParameter[] parm)
{
conn.Open();
       //最后一个参数为输出参数
parm[parm.Length - ].Direction = ParameterDirection.Output;
using (SqlCommand cmd = new SqlCommand(ProcName,conn))
{
cmd.CommandType = CommandType.StoredProcedure;
if (parm != null)
{
cmd.Parameters.AddRange(parm);
} int result = cmd.ExecuteNonQuery();
}
conn.Close();
       //返回输出的参数,看存储过程中定义的输出参数是什么类型,这里就转换成什么类型
return (bool)parm[parm.Length - ].Value;
}

以上为sqlhelper里面使用存储过程的方法.

如果调用呢?

     public object ZiJian(int uid,int cid,int jid)
{
SqlParameter[] parameter = {
new SqlParameter("@uid",SqlDbType.Int),
new SqlParameter("@cid",SqlDbType.Int),
new SqlParameter("@jid",SqlDbType.Int),
new SqlParameter("@result",SqlDbType.Bit)
};
parameter[].Value = uid;
parameter[].Value = cid;
parameter[].Value = jid;
parameter[].Direction = ParameterDirection.Output;
return SqlHelper.Proc($"Proc_Zijian",parameter);
}

因为存储过程中的参数不固定,所以在这里把使用存储过程的参数组成一个集合,最后一个为输出参数,所以不需要赋值,但是需要指出他是输出参数.parameter[3].Direction = ParameterDirection.Output;

上一篇:即时通讯App怎样才能火?背后的技术原理,可以从这5个角度切入


下一篇:移动跨平台开发框架Ionic开发一个新闻阅读APP