zoukankan      html  css  js  c++  java
  • C#通过执行sql语句的方式执行存储过程,得到输出参数

    一般情况下我们是这样执行存储过程的,下面方式也可以获取返回值

     public static DataTable ExecuteStoredPro(string storeName, out int resultcount, string title, int pageindex, int pagesize)
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = storeName;
                        SqlParameter[] para ={
                                         new SqlParameter("@title",SqlDbType.Int),
                                         new SqlParameter("@pageindex",SqlDbType.Int),
                                         new SqlParameter("@pagesize",SqlDbType.Int),
                                         new SqlParameter("@result_value",SqlDbType.Int)
    
                };
    
                        para[0].Value = title;
                        para[1].Value = pageindex;
                        para[2].Value = pagesize;
                        para[3].Direction = ParameterDirection.Output; //设定参数的输出方向  
    
                        cmd.Parameters.AddRange(para);// 将参数加入命令对象  
                        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                        DataTable dt = new DataTable();
                        adapter.Fill(dt);
    
                        resultcount = Convert.ToInt32(cmd.Parameters[3].Value);
    
    
                        return dt; 
                       
    
                    }
                }
    
            }

    但是在一些情况下,我们不能像上面直接通过存储过程名执行存储过程,我们可以像执行sql语句一样执行存储过程,这样获取返回值也有些区别,如下

     public string FileApprove(FileApproveMetadata data)
            {
                SqlParameter para = new SqlParameter() { ParameterName = "@error",DbType= DbType.String, Size=500};
                para.Direction = ParameterDirection.Output;//这个必须加,sql语句中的output也要加
    
                SqlParameter[] param = { new SqlParameter() { ParameterName= "@projectId", Value=data.ProjectId },
                    new SqlParameter() { ParameterName = "@taskId", Value = data.TaskId },
                    new SqlParameter() { ParameterName = "@subTaskId", Value = data.SubTaskId },
                    new SqlParameter() { ParameterName = "@create_EmpId", Value = data.Create_EmpId },
                    new SqlParameter() { ParameterName = "@fileIds", Value = data.FileIds },
                    new SqlParameter() { ParameterName = "@fileapprovers", Value = data.Fileapprovers },
                    new SqlParameter() { ParameterName = "@remark", Value = data.Remark },para};
               
    //直接写执行语句 CurrentRepository.ExecuteSql(
    @"exec [proc_Flow_AddFileApprove] @projectId,@taskId,@subTaskId,@create_EmpId,@fileIds, @fileapprovers,@remark,@error output", param); return para.Value.ToString(); }
  • 相关阅读:
    php之面向对象(2)
    PHP ON 阿里云的环境配置攻略
    InitPHP框架搭建高可用WEB应用
    PHP移动互联网开发笔记(6)——MySQL数据库基础回顾[1]
    5 个不用 Bootstrap 的理由
    http中get与post的区别
    django的CSRF保护机制
    博客园项目开发中的难点
    centos7.5静态网页基于nginx建站(python3.6 mysql5.7 nginx安装以及配置)
    python3面向对象常用特性知识点
  • 原文地址:https://www.cnblogs.com/zxking/p/7989061.html
Copyright © 2011-2022 走看看