您的位置:首页 > 数据库

.net 调用 sql server 自定义函数,并输出返回值

2012-05-10 12:28 591 查看
数据库结构:





表内的数据:





自定义函数: 递归查出 树下所有节点 ,参数是 父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();


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

}

实现的效果如下:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐