• C#存储过程 传入参数 传出参数 结果集


    作者:卞功鑫 转载请保留:http://www.cnblogs.com/BinBinGo/p/6400928.html 

    //1   连接字符串
                string connectionString
                    = "server=127.0.0.1;integrated security=true;database=MSPetShop4";
                // = "server=.;uid=sa;pwd=SQL@5;database=AdventureWorks2012";
                // = "server=.;user id=sa;password=SQL@5;database=AdventureWorks2012";
                //2 实例化数据库连接
                using(System.Data.SqlClient.SqlConnection connection = new SqlConnection(connectionString))
                {
                    //定义执行SQL语句,可以为select查询,也可以为存储过程,我们要的只是返回的结果集.
                    string sql = "p_proc_name";
    
                    //SqlCommand 表示数据库要执行的sql命令
                    System.Data.SqlClient.SqlCommand command = new SqlCommand(sql, connection);
                    //告知数据库现在要执行的是存储过程
                    //默认为标准SQL语句,可以不用设置.
                    command.CommandType = CommandType.StoredProcedure;
    
                    //提供存储过程参数(传入参数) 这里的名称@pin和存储过程中的保持一致
                    System.Data.SqlClient.SqlParameter pin = new SqlParameter("@pin", System.Data.SqlDbType.Int);
                    //参数赋值
                    pin.Value = 10;
                    //将上面的参数加入command中
                    command.Parameters.Add(pin);
                   //表值参数
                   System.Data.SqlClient.SqlParameter pids = new SqlParameter("@ids", ids); //ids 为datatable
                   pids.SqlDbType = SqlDbType.Structured;
                   pids.TypeName = "dbo.EntityIdCollection";//EntityIdcollection 为自定义类别 
                   command.Parameters.Add(pids);


    //提供存储过程参数(传出参数)这里的名称@pout和存储过程中的保持一致 System.Data.SqlClient.SqlParameter pout = new SqlParameter("@pout", System.Data.SqlDbType.Int); //声明为传出参数 Direction 参数方向 ,默认为传入参数 ParameterDirection.Input pout.Direction = ParameterDirection.Output; //将上面的参数加入command中 command.Parameters.Add(pout); //return 参数 名称@returnValue随便取,类型固定为int类型. System.Data.SqlClient.SqlParameter preturn = new SqlParameter("@returnValue",System.Data.SqlDbType.Int); //声明为传出参数 Direction 参数方向 ,默认为传入参数 ParameterDirection.Input preturn.Direction = ParameterDirection.ReturnValue; //return 在存储过程中隐藏的,但是在C#时要显式使用 command.Parameters.Add(preturn); //强大的SqlDataAdapter //可以使用 SqlDataAdapter(command) 属性实体化,也可以使用SqlDataAdapter(sql,connection)实例化. //SqlDataAdapter(command) 主要用于存储过程 //SqlDataAdapter(sql,connection) 主要用于select语句 System.Data.SqlClient.SqlDataAdapter adapter = new SqlDataAdapter(command); //用于接收adapter.Fill 返回的结果集 DataSet ds = new DataSet(); //返回集插入到 dataset ds 中 adapter.Fill(ds); //现在得到了三个东西 //一 存储过程的返回结果集 dataset //二 存储过程的 output 传出参数值 //三 存储过程的 return 值 int outputValue = (int)pout.Value; int returnValue = (int)preturn.Value; Console.WriteLine("返回了{0}个DataTable;outputValue 的值为{1};returnValue 的值为{2}", ds.Tables.Count,outputValue,returnValue); } Console.ReadLine();

    存储过程

    ALTER PROC p_proc_name
        (
          @pin INT ,
          @pout INT OUTPUT
        )
    AS --------执行用户自定义逻辑--------------
    
    
    
    
    ---------返回结果集 1----------------------
    
        SELECT  客户 ,
                产品 ,
                数量 ,
                金额 ,
                年龄
        FROM    dbo.A
    
    -----------返回结果集 2--------------------
    
        SELECT  客户 ,
                产品 ,
                数量 ,
                金额 ,
                年龄
        FROM    dbo.A
        WHERE   年龄 IS NOT NULL
    
    
      -----------设置output参数值-------------------  
      
        SET @pout = @pin * 100;
    
    --------------returnw值-------------
                   
        IF ( @pin <= 0 )
                     --return 如果没有写,其值默认为0 ,表示执行成功.
            RETURN -1;
                     --return 之后的语句不执行.
  • 相关阅读:
    聊聊CMDB的前世今生
    我是如何走上运维岗位的?谈谈新人入职运维发展的注意事项
    如何从生命周期的视角看待应用运维体系建设?
    标准化体系建设(下):如何建立基础架构标准化及服务化体系?
    标准化体系建设(上):如何建立应用标准化体系和模型?
    微服务架构时代,运维体系建设为什么要以“应用”为核心?
    Kubernetes容器化工具Kind实践部署Kubernetes v1.18.x 版本, 发布WordPress和MySQL
    Etcd常用运维命令
    Logstash生产环境实践手册(含grok规则示例和ELKF应用场景)
    Netflix业务运维分析和总结
  • 原文地址:https://www.cnblogs.com/BinBinGo/p/6400928.html
Copyright © 2020-2023  润新知