sqlserver创建存储过程、函数、
2011-02-26 21:39
381 查看
--有输入参数的存储过程--
create proc GetComment
(@commentid int)
as
select * from Comment where CommentID=@commentid
C# 调用有输入参数的存储过程
SqlConnection conn=new SqlConnection("Server=.;Database=MyDB;uid=sa;pwd=123456");
SqlCommand cmd=new SqlCommand();
cmd.Connection=conn;
cmd.CommandType=CommandType.StoredProcedure;
cmd.CommandText="GetComment";
cmd.Parameters.Clear();
cmd.Parameters.Add("@commentid",SqlDbType.Int).Value=1;
DataTable dt=new DataTable();
SqlDataAdapter da=new SqlDataAdapter(cmd);
da.Fill(dt);
GridView1.DataSource=dt;
GridView1.DataBind();
--有输入与输出参数的存储过程--
create proc GetCommentCount
@newsid int,
@count int output
as
select @count=count(*) from Comment where NewsID=@newsid
SqlConnection conn=new SqlConnection("Server=.;DataBase=MyDB;uid=sa;pwd=123456");
SqlCommand cmd=new SqlCommand();
conn.Open();
cmd.Connection=conn;
cmd.CommandType=CommandType.StoredProcedure;
cmd.CommandText="GetCommentCount";
cmd.Parameters.Clear();
cmd.Parameters.Add("@newsid",SqlDbType.Int).Value=2;
SqlParameter sp=new SqlParameter();
sp.ParameterName="@count";
sp.SqlDbType=SqlDbType.Int;
sp.Direction=ParameterDirection.Output;
cmd.Parameters.Add(sp);
Response.Write(sp.Value.ToString());
--返回单个值的函数--
create function MyFunction
(@newsid int)
returns int
as
begin
declare @count int
select @count=count(*) from Comment where NewsID=@newsid
return @count
end
SqlConnection conn=new SqlConnection("Server=.;Database=MyDB;uid=sa;pwd=123456");
SqlCommand cmd=new SqlCommand();
conn.Open();
cmd.Connection=conn;
cmd.CommandText="MyFunction";
cmd.CommandType=CommandType.StoredProcedure; -----这儿要设置为存储过程
cmd.Parameters.Add("@newsid",SqlDbType.Int).Value=2;
SqlParameter sp=new SqlParameter();
sp.ParameterName="@count";
sp.SqlDbType=SqlDbType.Int;
sp.Direction=ParameterDirection.ReturnValue;
cmd.Parameters.Add(sp);
cmd.ExecuteNonQuery();
Response.Write(sp.Value.ToString());
--调用方法--
declare @count int
exec @count=MyFunction 2
print @count
--返回值为表的函数--
Create function GetFunctionTable
(@newsid int)
returns table
as
return
(select * from Comment where NewsID=@newsid)
--返回值为表的函数的调用--
select * from GetFunctionTable(2)
SqlConnection conn=new SqlConnection("Server=.;Database=MyDB;uid=sa;pwd=123456");
SqlCommand cmd=new SqlCommand();
conn.Open();
cmd.Connection=conn;
cmd.CommandType=CommandType.Text;//注意这儿设置为文本
cmd.CommandText="Select * from GetFunctionTable(@newsid)";
SqlDataReader dr=cmd.ExecuteReader();
DataTable dt=new DataTable();
dt.Load(dt);
GridView1.DataSource=dt;
GridView1.DataBind();
create proc GetComment
(@commentid int)
as
select * from Comment where CommentID=@commentid
C# 调用有输入参数的存储过程
SqlConnection conn=new SqlConnection("Server=.;Database=MyDB;uid=sa;pwd=123456");
SqlCommand cmd=new SqlCommand();
cmd.Connection=conn;
cmd.CommandType=CommandType.StoredProcedure;
cmd.CommandText="GetComment";
cmd.Parameters.Clear();
cmd.Parameters.Add("@commentid",SqlDbType.Int).Value=1;
DataTable dt=new DataTable();
SqlDataAdapter da=new SqlDataAdapter(cmd);
da.Fill(dt);
GridView1.DataSource=dt;
GridView1.DataBind();
--有输入与输出参数的存储过程--
create proc GetCommentCount
@newsid int,
@count int output
as
select @count=count(*) from Comment where NewsID=@newsid
SqlConnection conn=new SqlConnection("Server=.;DataBase=MyDB;uid=sa;pwd=123456");
SqlCommand cmd=new SqlCommand();
conn.Open();
cmd.Connection=conn;
cmd.CommandType=CommandType.StoredProcedure;
cmd.CommandText="GetCommentCount";
cmd.Parameters.Clear();
cmd.Parameters.Add("@newsid",SqlDbType.Int).Value=2;
SqlParameter sp=new SqlParameter();
sp.ParameterName="@count";
sp.SqlDbType=SqlDbType.Int;
sp.Direction=ParameterDirection.Output;
cmd.Parameters.Add(sp);
Response.Write(sp.Value.ToString());
--返回单个值的函数--
create function MyFunction
(@newsid int)
returns int
as
begin
declare @count int
select @count=count(*) from Comment where NewsID=@newsid
return @count
end
SqlConnection conn=new SqlConnection("Server=.;Database=MyDB;uid=sa;pwd=123456");
SqlCommand cmd=new SqlCommand();
conn.Open();
cmd.Connection=conn;
cmd.CommandText="MyFunction";
cmd.CommandType=CommandType.StoredProcedure; -----这儿要设置为存储过程
cmd.Parameters.Add("@newsid",SqlDbType.Int).Value=2;
SqlParameter sp=new SqlParameter();
sp.ParameterName="@count";
sp.SqlDbType=SqlDbType.Int;
sp.Direction=ParameterDirection.ReturnValue;
cmd.Parameters.Add(sp);
cmd.ExecuteNonQuery();
Response.Write(sp.Value.ToString());
--调用方法--
declare @count int
exec @count=MyFunction 2
print @count
--返回值为表的函数--
Create function GetFunctionTable
(@newsid int)
returns table
as
return
(select * from Comment where NewsID=@newsid)
--返回值为表的函数的调用--
select * from GetFunctionTable(2)
SqlConnection conn=new SqlConnection("Server=.;Database=MyDB;uid=sa;pwd=123456");
SqlCommand cmd=new SqlCommand();
conn.Open();
cmd.Connection=conn;
cmd.CommandType=CommandType.Text;//注意这儿设置为文本
cmd.CommandText="Select * from GetFunctionTable(@newsid)";
SqlDataReader dr=cmd.ExecuteReader();
DataTable dt=new DataTable();
dt.Load(dt);
GridView1.DataSource=dt;
GridView1.DataBind();
相关文章推荐
- Mysql Query Browser下创建存储过程和自定义函数
- SqlServer判断数据库、表、存储过程、函数是否存在
- SQLServer中存储过程StoredProcedure创建及C#调用(转)
- SQLserver 在存储过程中创建函数,创建视图
- sql判断库、表、列、存储过程、视图、函数存在性与创建
- mysql存储过程与函数------创建存储过程与函数
- SQL查询表、视图、存储过程、函数的创建和变更时间(收藏)
- (原)用于 SQLServer 自动创建单号的存储过程
- mysql 触发器采坑记录(mysql 触发器采坑记录 由于不了解mysql创建触发器要记录创建者IP,导致更改创建者IP后,触发器不可执行。 当创建触发器(视图、存储过程、函数)时,如果没有)
- Oracle创建存储过程、创建函数、创建包基本语法
- Oracle和SQLServer分别使用函数和存储过程返回结果集
- sqlserver存储过程如何调用自定义函数
- sqlserver中创建包含事务的存储过程
- sqlserver 导入数据出现 无法创建 OLE DB 取值函数。请查看列元数据是否有效
- SQLServer 中存储过程返回的三种方式( 包括存储过程的创建, 在存储过程中调用, 在VS中调用的方法)
- SQlserver 创建函数实现只取某个字段的数字部分
- SqlServer查看对象(表、存储过程、函数)在哪些地方被引用或引用哪些地方
- 浅谈 sqlserver 和 mysql存储过程、函数的区别
- sqlserver中用存储过程创建sql任务(job)
- MYSQL的基本使用【表的创建和修改、视图、函数、存储过程、触发器和事件调度器】