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();
相關文章
相關標籤/搜索