zoukankan      html  css  js  c++  java
  • .net 调用 sql server 自定义函数,并输出返回值

    数据库结构:

    image

    表内的数据:

    image

    自定义函数: 递归查出 树下所有节点 ,参数是 父id

     create  function sss(@id as int)
      returns @t table
      (
        id int not null,
        name int not null,
        pid int null
      )
      as
      begin
      declare @lay as int;
      insert into @t 
      select * from tree where pid =@id;
      
      select @lay = min(id) from tree where pid =@id; --第一次 @lay=5
      
      while @lay is not null
      begin
      
        insert into @t 
        select * from sss(@lay);
        
        select @lay=min(id) from tree
        where id>@lay and pid=@id
      end
      return;
      end
      go
      
      
     
    .net代码:
     string cons = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();
    
            using (SqlConnection con=new SqlConnection(cons))
            {
                if (con.State==ConnectionState.Closed)
                {
                    con.Open();
                    
                }
    
                string sql = "select * from sss(@id)";
                SqlCommand cmd = new SqlCommand(sql,con);
              
                cmd.CommandType = CommandType.Text;
               
    
                cmd.Parameters.Add(new SqlParameter("@id", DbType.Int32)).Value = 4;
                cmd.Parameters.Add("@re",DbType.String);
                cmd.Parameters["@re"].Direction = ParameterDirection.ReturnValue;
    
                SqlDataReader dr = cmd.ExecuteReader();
    
                while (dr.Read())
                {
                    int i = 0;
                    Response.Write(dr[0].ToString() + "\t\t\t" +dr[1].ToString() +"\t\t\t"+  dr[2].ToString() + "</br>");
                    i++;
                    
                }
    
    
    
    
                con.Close();

    }

    实现的效果如下:

    image

  • 相关阅读:
    04Windows频繁打开和关闭端口可能引发的问题 | 07.杂项
    04WebFinger的利用 | 02.技术预研 | Social
    Hunch:自动问答和决策机
    03PubSubHubbub 和 twisted 的 Persistent connections 能力 | 07.杂项 | Python
    01获取 Twitter User Profile 的三条路径 | 07.杂项
    大中华之事件监测
    一个如此简单的杀手级应用
    07爬虫的多线程调度 | 01.数据抓取 | Python
    02Twisted 构建 Web Server 的 Socket 长链接问题 | 07.杂项 | Python
    关于Cutt.com关于Topic Engine
  • 原文地址:https://www.cnblogs.com/soundcode/p/2680119.html
Copyright © 2011-2022 走看看