设为首页 加入收藏

TOP

数据库存储过程简介与实例
2015-07-24 11:58:42 来源: 作者: 【 】 浏览:20
Tags:数据库 存储 过程 简介 实例

一、存储过程与函数的区别:

  1.一般来说,存储过程实现的功能要复杂一点,而函数的实现的功能针对性比较强。

  2.对于存储过程来说可以返回参数(output),而函数只能返回值或者表对象。

  3.存储过程一般是作为一个独立的部分来执行,而函数可以作为查询语句的一个部分来调用,由于函数可以返回一个表对象,因此它可以在查询语句中位于FROM关键字的后面。

?

二、存储过程的优点:

  1.执行速度更快 – 在数据库中保存的存储过程语句都是编译过的

  2.允许模块化程序设计 – 类似方法的复用

  3.提高系统安全性 – 防止SQL注入

  4.减少网络流通量 – 只要传输存储过程的名称

?

系统存储过程一般以sp开头,用户自定义的存储过程一般以usp开头

?

三、定义存储过程语法,"[" 里面的内容表示可选项

  create proc 存储过程名

  @参数1 数据类型 [=默认值] [output],

  @参数2 数据类型 [=默认值] [output],

  ...

  as

  SQL语句

?

四、简单的一个例子

  定义存储过程:

  create proc usp_StudentByGenderAge

  @gender nvarchar(10) [='男'],

  @age int [=30]

  as

  select * from MyStudent where FGender=@gender and FAge=@age

?

  执行存储过程:

  Situation One(调用默认的参数):

  exec usp_StudentByGenderAge

  Situation Two(调用自己指定的参数):

  exec usp_StudentByGenderAge '女',50

  或者指定变量名 exec usp_StudentByGenderAge @age=50,@gender='女'

?

  对存储过程进行修改

  alter proc usp_StudentByGenderAge

  @gender nvarchar(10) [='男'],

  @age int [=30],

  --加output表示该参数是需要在存储过程中赋值并返回的

  @recorderCount int output

  as

  select * from MyStudent where FGender=@gender and FAge=@age

  set @recorderCount=(select count(*) from MyStudent where FGender=@gender and FAge=@age)

?

--output参数的目的,就是调用者需要传递一个变量进来,然后在存储过程中为该变量完成赋值工作,存储过程执行完成以后,将执行的对应结果返回给传递进来的变量。(与C#中的out原理一模一样)

?

调用(记住这里的语法!)因为该存储过程前面还有其他参数,所以要把 @recorderCount写上,该存储过程执行后,相当与完成了以上的查询工作,同时将查询结果得到的条数赋值给了@count变量。(@count是当做参数传给usp_StudentByGenderAge,当存储过程执行完毕以后,将得到的条数返回给@count)

  declare @count int

  exec usp_StudentByGenderAge @recorderCount=@count output

  print @count

?

五、使用存储过程完成分页

1、存储过程代码

  create proc usp_page

  @page int, ---一页显示多少条记录

  @number int, ---用户选择了第几页数据

as

  begin

  select * from

  --小括号里面内容是专门得到排列好的序号

  (

    select ROW_NUMBER() over(order by(Fid)) as number

    from MyStudent

  ) as t

  where t.number>= (@number-1)*@page+1 and t.number<=@number*@page

   end

2、实现分页效果对应的ADO.NET代码:

 private void button1_Click(object sender, EventArgs e) 
  {
  string connStr = @"server=.\sqlexpress;database=MyDB;integrated security=true";
   using (SqlConnection conn = new SqlConnection(connStr))
   {
     //打开数据库连接
     conn.Open();
     //用存储过程名作为Command处理的对象
     string usp = "usp_page";
     using (SqlCommand cmd = new SqlCommand(usp, conn))
     {
       //执行的是存储过程语句
       cmd.CommandType = CommandType.StoredProcedure;
        //textBox1.Text是指显示多少条记录
       cmd.Parameters.AddWithValue("@page", textBox1.Text.Trim());
       //textBox.Text是指用户选择了第几页
       cmd.Parameters.AddWithValue("@number", textBox2.Text.Trim());
       //用list作为数据源来实现
       List p = new List();
       using (SqlDataReader reader = cmd.ExecuteReader())
       {
         if (reader.HasRows)
         {
           while (reader.Read())
           {
             Person p1 = new Person();
             p1.FName = reader.GetString(1);
             p1.FAge = reader.GetInt32(2);
             p1.FGender = reader.GetString(3);
             p1.FMath = reader.GetInt32(4);
             p1.FEnglish = reader.GetInt32(5);
             p.Add(p1);
           }
         }
       }
       dataGridView1.DataSource = p;
     }
   }
 }
下面是自定义的Person类
class Person 
 {
public string FName { get; set; }
public int FAge { get; set; }
public string FGender { get; set; }
public int FMath { get; set; }
public int FEnglish { get; set; }
}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇归档日志 下一篇异常处理

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: