• 后台获取存储过程返回的数据(转)


    1.OUPUT参数返回值 例: 向Order表插入一条记录,返回其标识

    CREATEPROCEDURE[dbo].[nb_order_insert]( @o_buyeridint , @o_idbigint OUTPUT ) AS BEGIN SET NOCOUNT ON; BEGIN INSERTINTO[Order](o_buyerid ) VALUES (@o_buyerid ) SET@o_id=@@IDENTITY END END

    存储过程中获得方法:

    DECLARE@o_buyeridint DECLARE@o_idbigint EXEC[nb_order_insert]@o_buyerid ,o_id bigint

    2.RETURN过程返回值

    CREATEPROCEDURE[dbo].[nb_order_insert]( @o_buyeridint , @o_idbigint OUTPUT ) AS BEGIN SET NOCOUNT ON; IF(EXISTS(SELECT*FROM[Shop]WHERE[s_id]=@o_shopid)) BEGIN INSERTINTO[Order](o_buyerid ) VALUES (@o_buyerid ) SET@o_id=@@IDENTITY RETURN1 — 插入成功返回1 END ELSE RETURN0 — 插入失败返回0 END

    存储过程中的获取方法

    DECLARE@o_buyeridint DECLARE@o_idbigint DECLARE@resultbit EXEC@result=[nb_order_insert]@o_buyerid ,o_id bigint

    3.SELECT 数据集返回值

    CREATEPROCEDURE[dbo].[nb_order_select]( @o_idint ) AS BEGIN SET NOCOUNT ON; SELECT o_id,o_buyerid FROM[Order] WHERE o_id =@o_id GO

    存储过程中的获取方法 (1)、使用临时表的方法

    CREATETABLE[dbo].[Temp]( [o_id][bigint]IDENTITY(1,1) NOTFORREPLICATIONNOTNULL, [o_buyerid][int]NOTNULL ) INSERT[Temp]EXEC[nb_order_select]@o_id – 这时 Temp 就是EXEC执行SELECT 后的结果集 SELECT*FROM[Temp] DROP[Temp] — 删除临时表

    (2)、速度不怎么样.(不推荐)

    SELECT*fromopenrowset(’provider_name','Trusted_Connection=yes’,'exec nb_order_select’)

    1.获取Return返回值

    //存储过程 
    //Create PROCEDURE MYSQL
    //     @a int,
    //     @b int
    //AS /
    /     return @a + @b
    //GO
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
    conn.Open();
    SqlCommand MyCommand = new SqlCommand("MYSQL", conn);
    MyCommand.CommandType = CommandType.StoredProcedure;
    MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int));
    MyCommand.Parameters["@a"].Value = 10;
    MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int));
    MyCommand.Parameters["@b"].Value = 20;
    MyCommand.Parameters.Add(new SqlParameter("@return", SqlDbType.Int));
    MyCommand.Parameters["@return"].Direction = ParameterDirection.ReturnValue;
    MyCommand.ExecuteNonQuery();
    Response.Write(MyCommand.Parameters["@return"].Value.ToString());

    2.获取Output输出参数值

    //存储过程
    //Create PROCEDURE MYSQL
    //     @a int,
    //     @b int,
    //     @c int output
    //AS
    //     Set @c = @a + @b
    //GO
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString()); conn.Open(); SqlCommand MyCommand = new SqlCommand("MYSQL", conn); MyCommand.CommandType = CommandType.StoredProcedure; MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int)); MyCommand.Parameters["@a"].Value = 20; MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int)); MyCommand.Parameters["@b"].Value = 20; MyCommand.Parameters.Add(new SqlParameter("@c", SqlDbType.Int)); MyCommand.Parameters["@c"].Direction = ParameterDirection.Output; MyCommand.ExecuteNonQuery(); Response.Write(MyCommand.Parameters["@c"].Value.ToString());

    C#接收存储过程返回值:

    public static int User_Add(User us) 

    {        

      int iRet;         

    SqlConnection conn = new SqlConnection(Conn_Str);         

    SqlCommand cmd = new SqlCommand("User_Add", conn);         

    cmd.CommandType = CommandType.StoredProcedure;         

    cmd.Parameters.AddWithValue("@UName", us.UName);         

    cmd.Parameters.AddWithValue("@UPass", us.UPass);         

    cmd.Parameters.AddWithValue("@PassQuestion", us.PassQuestion);         

    cmd.Parameters.AddWithValue("@PassKey", us.PassKey);         

    cmd.Parameters.AddWithValue("@Email", us.Email);         

    cmd.Parameters.AddWithValue("@RName", us.RName);         

    cmd.Parameters.AddWithValue("@Area", us.Area);         

    cmd.Parameters.AddWithValue("@Address", us.Address);         

    cmd.Parameters.AddWithValue("@ZipCodes", us.ZipCodes);         

    cmd.Parameters.AddWithValue("@Phone", us.Phone);         

    cmd.Parameters.AddWithValue("@QQ", us.QQ);         

    cmd.Parameters.Add("@RETURN_VALUE", "").Direction = ParameterDirection.ReturnValue;               

    try         

    {             

    conn.Open();             

    cmd.ExecuteNonQuery();             

    iRet = (int)cmd.Parameters["@RETURN_VALUE"].Value;         

    }

    catch (SqlException ex)         

    {             

    throw ex;         

    }         

    finally         

    {             

    conn.Close();         

    }         

    return iRet;     

    }

     

    C#接收存储过程输出参数:

        public static decimal Cart_UserAmount(int UID)    

    {        

    decimal iRet;        

    SqlConnection conn = new SqlConnection(Conn_Str);        

    SqlCommand cmd = new SqlCommand("Cart_UserAmount", conn);        

    cmd.CommandType = CommandType.StoredProcedure;        

    cmd.Parameters.AddWithValue("@UID", UID);       

    cmd.Parameters.Add("@Amount", SqlDbType.Decimal).Direction=ParameterDirection.Output;        

    try        

    {            

    conn.Open();            

    cmd.ExecuteNonQuery();            

    iRet = (decimal)cmd.Parameters["@Amount"].Value;        

    }        

    catch (SqlException ex)        

    {           

      throw ex;        

    }        

    finally        

    {            

    conn.Close();        

    }        

    return iRet;    

    }

  • 相关阅读:
    Ajax_原生ajax写法、理解异步请求、js单线程+事件队列、封装原生ajax
    Ajax_实现动态网站的技术、php语法、php接口、前端渲染和后端渲染
    Ajax_Apache访问资源文件的权限配置、资源存放路径配置、配置虚拟主机、动态网站静态网站区别
    Ajax_Ajax、客户端、服务器端的认识与比较、服务器环境的搭建wamp
    jquery_jquery插件、jqueryUI使用方法、自定义jquery插件(一个插件其实就是一个功能)
    jquery_jquery事件绑定和解绑、链式编程、jquery中的主动遍历each、多库共存
    jquery_jquery动态创建元素及应用
    jquery_jquery动画
    jquery_jquery样式操作、链式编程
    jquery_认识jquery好处、选择器、小案例
  • 原文地址:https://www.cnblogs.com/spider024/p/2973213.html
Copyright © 2020-2023  润新知