导航

C#存储过程中传入传出参数

Posted on 2017-02-15 02:17  yiyishuitian  阅读(2077)  评论(0编辑  收藏  举报

 作者:卞功鑫  ,转载请保留http://www.cnblogs.com/BinBinGo/p/6399847.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 实例化数据库连接
            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' 
                     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 = 10;
                //将上面的参数加入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);


                //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 obj = pout.Value;

               int p_result = Convert.ToInt32(obj);




                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,执行的结果为:{0}",p_result);
            }
            catch(System.Data.SqlClient.SqlException exception)
            {
                Console.WriteLine(exception.Message);
            }

            finally
            {
                //4 注销连接
                connection.Dispose();
                Console.WriteLine("成功断开数据计库MSPetShop4");
            }
            Console.ReadLine();