• C#存储过程中return参数


                //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 实例化数据库连接
                System.Data.SqlClient.SqlConnection connection = new SqlConnection(connectionString);
    
                //也可以先实例化
                //System.Data.SqlClient.SqlConnection connection = new SqlConnection();
                //然后再设置ConnectionString 属性.
                //connection.ConnectionString = connectionString;
    
                try
                {
                    //3 打开连接
                    connection.Open();
                    Console.WriteLine("成功连接数据计库MSPetShop4");
                    //4 数据访问对象
                    //sql字符串存储过程
    
                    string sql = "p_proc_name";
                    /*
                       CREATE PROC p_proc_name
                        (
                          @pin INT ,
                          @pout INT OUTPUT
                        )
                      AS
                        DELETE  FROM dbo.A
                        WHERE   客户 = 'biangongxin' 
                     
                        IF ( @pin <= 0 )
                     --return 如果没有写,其值默认为0 ,表示执行成功.
                            RETURN -1;
                     --return 之后的语句不执行.
                        SET @pout = @pin * 100;
                     
                     
                     */
                    //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 = 0;
                    //将上面的参数加入command中
                    command.Parameters.Add(pin);
    
                    //提供存储过程参数(传出参数)这里的名称@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);
    
                   
    
    
    
                    //ExecuteNonQuery 非查询语句
                    //默认工作在自动事务之下,直接提交
                    //执行sql DML 之前,手动开启
                    System.Data.SqlClient.SqlTransaction trans =  connection.BeginTransaction();
                    //设置命令所属的事务管理
                    command.Transaction = trans;
                    int result =  command.ExecuteNonQuery();
                    Console.WriteLine(result);
    
                    // 传出参数 存储过程执行过之后,可以得到传出的参数(存储过程执行的时候,会把sql中的 output的这个参数的值赋值给C#中的 pout)
                    //传出参数的类型为 object 类型
                   object obj = pout.Value;
    
                   //return 参数 存储过程执行过之后,可以得到传出的参数(存储过程执行的时候,会把sql中的 return的这个参数的值赋值给C#中的 preturn
                   //return 参数类型为固定int 类型
                   int returnValue = (int)preturn.Value;
    
    
    
                    Console.Write("SQL命令已经提交,但是事务还未提交,是否继续执行(Y/N)");
                    string ans = Console.ReadLine();
                    //提交与否@pout值的返回值始终为1000,影响的只是 SQL的 DML操作
                    if (ans.Substring(0, 1).ToUpper() == "Y")
                    {
    
                        //提交事务
                        trans.Commit();
                    }
                    else
                    {
                        //回滚事务;
                        trans.Rollback();
                    }
    
                    Console.WriteLine("存储过程p_proc_name,return结果为:{0}", returnValue);
                   
                }
                catch(System.Data.SqlClient.SqlException exception)
                {
                    Console.WriteLine(exception.Message);
                }
    
                finally
                {
                    //4 注销连接
                    connection.Dispose();
                    Console.WriteLine("成功断开数据计库MSPetShop4");
                }
                Console.ReadLine();
  • 相关阅读:
    Android学习之adb异常处理
    Android学习之多触点滑动
    RN animated帧动画
    RN animated组动画
    RN animated缩放动画
    RN Animated透明度动画
    Eclipse ADT中的logcat不显示解决方法
    RadioButton使用
    PropTypes使用
    SegmentedControlIOS使用
  • 原文地址:https://www.cnblogs.com/BinBinGo/p/6399851.html
Copyright © 2020-2023  润新知