zoukankan      html  css  js  c++  java
  • 存储过程与函数

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

      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代码:

    复制代码
    复制代码
    复制代码
     1 private void button1_Click(object sender, EventArgs e) 
      {
     2   string connStr = @"server=.sqlexpress;database=MyDB;integrated security=true";
     3   using (SqlConnection conn = new SqlConnection(connStr))
     4   {
     5     //打开数据库连接 6     conn.Open();
     7     //用存储过程名作为Command处理的对象 8     string usp = "usp_page";
     9     using (SqlCommand cmd = new SqlCommand(usp, conn))
    10     {
    11       //执行的是存储过程语句12       cmd.CommandType = CommandType.StoredProcedure;
            //textBox1.Text是指显示多少条记录
    13       cmd.Parameters.AddWithValue("@page", textBox1.Text.Trim());
    14       //textBox.Text是指用户选择了第几页15       cmd.Parameters.AddWithValue("@number", textBox2.Text.Trim());
    16       //用list作为数据源来实现17       List<Person> p = new List<Person>();
    18       using (SqlDataReader reader = cmd.ExecuteReader())
    19       {
    20         if (reader.HasRows)
    21         {
    22           while (reader.Read())
    24           {
    25             Person p1 = new Person();
    26             p1.FName = reader.GetString(1);
    27             p1.FAge = reader.GetInt32(2);
    28             p1.FGender = reader.GetString(3);
    29             p1.FMath = reader.GetInt32(4);
    30             p1.FEnglish = reader.GetInt32(5);
    31             p.Add(p1);
    32           }
    33         }
    34       }
    35       dataGridView1.DataSource = p;
    36     }
    37   }
    38 }
  • 相关阅读:
    HTML静态网页 标签、表格
    SQL server 视图、范式
    SQL server 触发器
    SQL server while语句、存储过程
    SQL server 子查询、设置主键外键、变量及变量查询
    Excel单元格内容拆分、合并
    Excel日期格式调整
    Excel单元格内容批量加前缀
    Microsoft SQL Server, 错误:4064的解决方法 (转载)
    .net调用web邮箱发送邮件(转载)
  • 原文地址:https://www.cnblogs.com/TBW-Superhero/p/5569500.html
Copyright © 2011-2022 走看看