什么是存储过程
存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL语句集,存储在数据库在那个,一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字给出参数(如果该存储过程带有参数)来执行它。
写存储过程
目前使用某种编程语言来创建存储过程,还没有看到,貌似唯一方法就是在数据库里面提前写好,这样子就显现了一些劣势,没法随意的增删改,尤其维护起来困难,首先必须是数据库管理员,其次牵一发而动全身,如若需求变更,增加字段,那么修改的地方就多了。然后 数据库的可移植性差。
在SQLServer里面编写存储过程
最简单的存储过程
create proc ss
as
select * from Bill
go
exec ss
调用存储过程
- C#调用存储过程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
string strsql = "Data Source=Jason;Initial Catalog =Dormitory;Integrated Security=True";
string sql = "ss";
SqlConnection constr = new SqlConnection(strsql);
SqlCommand comStr = new SqlCommand(sql, constr);
comStr.CommandType = System.Data.CommandType.StoredProcedure;//主要就是这句
constr.Open();
SqlDataAdapter SqlDataAdapter1 = new SqlDataAdapter(comStr);
DataTable dt = new DataTable();
SqlDataAdapter1.Fill(dt);
constr.Close();
}
}
}
if(exists(select * from sys.objects where name='proc_get_student'))
drop proc proc_get_student
go
create proc proc_get_student
as
select * from Users;
--调用、执行存储过程