码迷,mamicode.com
首页 > 数据库 > 详细

sql 储存过程的使用

时间:2014-05-26 07:58:41      阅读:379      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   ext   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-<span style="background-color: rgb(255, 255, 0);">-获取所有数据 根据自定义函数传人类型id返回类型名称</span>
USE [Cloths]
GO
/****** Object:  StoredProcedure [dbo].[Proc_all]    Script Date: 05/23/2014 12:10:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE   [dbo].[Proc_all]
AS
BEGIN
    SET NOCOUNT ON;
   select ClothColorId ,Name ,<span style="background-color: rgb(255, 0, 0);">dbo.myfun1(TypeId)</span> as typename from dbo.ClothColors;
END

  这些储存过程都是修改的,吧ALTER改为create 就可以在数据库中创建 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
USE [Cloths]
GO
/****** Object:  UserDefinedFunction [dbo].[myfun1]    Script Date: 05/23/2014 13:53:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
  
<span style="background-color: rgb(255, 255, 0);"> --自定义函数</span>
ALTER FUNCTION  [dbo].[myfun1]
(  
 @a varchar(50)
      
)
 returns varchar(50)
as
begin
 declare @name varchar(50);
 select @name=[types].name from [types] where typeid=@a;
 return @name;
end

  //既有传人参数,又有返回参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
USE [Cloths]
GO
/****** Object:  StoredProcedure [dbo].[Proc_all]    Script Date: 05/23/2014 13:56:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO<br>--<span style="background-color: rgb(255, 255, 0);">储存过程</span>
ALTER PROCEDURE   [dbo].[Proc_all]
(
 @name varchar(50), --<span style="background-color: rgb(255, 255, 0);">默认传人</span>
 @count int output
)
AS
BEGIN
    SET NOCOUNT ON;
   select ClothColorId ,Name ,dbo.myfun1(TypeId) as typename from dbo.ClothColors where Name=@name;
   select @count=COUNT(1) from dbo.ClothColors;
   
END

  //是c#实现传人参数和返回参数

1
2
3
4
5
6
7
8
9
10
SqlCommand comm = new SqlCommand();
          comm.Connection = new SqlConnection("Data Source=.;Initial Catalog=Cloths;Integrated Security=True");
          comm.CommandType = CommandType.StoredProcedure;
          comm.CommandText = "Proc_all";
          comm.Parameters.Add(new SqlParameter("@name", "大红"));//传人参数
          comm.Parameters.Add(new SqlParameter("@count",DbType.Int32));
          comm.Parameters["@count"].Direction = <span style="background-color: rgb(255, 255, 0);">ParameterDirection.Output; //这个必须写,不写不会返回要传出的参数</span>
          SqlDataAdapter sda = new SqlDataAdapter(comm);
          DataSet ds=new DataSet ();
          sda.Fill(ds);

  

sql 储存过程的使用,布布扣,bubuko.com

sql 储存过程的使用

标签:style   c   class   blog   code   ext   

原文地址:http://www.cnblogs.com/wlzhang/p/3747639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!