存储过程使用篇
1. 在SQL中执行
执行已创建的存储过程使用EXECUTE 命令其语法如下
{[@return_statur=]
{procedure_name[;number] | @procedure_name_var}
[[@parameter=] {value | @variable [OUTPUT] | [DEFAULT] [,…n]
[WITH RECOMPILE]
各参数的含义如下
@return_status
是可选的整型变量用来存储存储过程向调用者返回的值
@procedure_name_var
是一变量名用来代表存储过程的名字
其它参数据和保留字的含义与Create PROCEDURE 中介绍的一样
例如我们有一个存储过程名为student_list_info要执行,在查询分析器中你只要写
Execute student_list_info
Go
就可以了
如果存储过程中包含有返回值的存储过程,那我们就必须指定参数值.看下面这个例子
此例摘自《SQLserver程序员指南》一书
as
select ord_num, ord_date, payterms, title_id, qty
from sales
where stor_id = @stor_id
select @sum = sum qty
from sales
where stor_id = @stor_id
go
要执行此存储过程,则我们要指定参数@sort_id,@sum的参数值.
execute salequa '7131',@totalqua output
if @totalqua<=50
select '销售信息'='销售等级为3 销售量为'+rtrim cast @totalqua as varchar 20
if @totalqua>50 and @totalqua<=100
select '销售信息'='销售等级为2 销售量为'+rtrim cast @totalqua as varchar 20
if @totalqua>100
select '销售信息'='销售等级为1 销售量为'+rtrim cast @totalqua as varchar 20
运行结果为
-------------------- --------------------------- ------------ -------- ------
N914008 1994-09-14 00:00:00.000 Net 30 PS2091 20
N914014 1994-09-14 00:00:00.000 Net 30 MC3021 25
P3087a 1993-05-29 00:00:00.000 Net 60 PS1372 20
P3087a 1993-05-29 00:00:00.000 Net 60 PS2106 25
P3087a 1993-05-29 00:00:00.000 Net 60 PS3333 15
P3087a 1993-05-29 00:00:00.000 Net 60 PS7777 25
6 row s affected
销售信息
-----------------------------------------
销售等级为1 销售量为130
2. 在ASP.NET中使用存储过程
要在ASP.Net(这里以c#为说明)中使用存储过程,首先要查看一下页面中是否引用了 System.Data.Sqlclient;当然数据库连接是必不可少的。我们知,一般我们在Asp.Net中调用数据的步骤是这样的:
新建一个数据库连接对象(一般用SqlConnection)→用Open()方法打开我们要操作的数据库→创建一个SqlCommand或SqlDataAdapter对象→对SQL命令或存储过程用ExecuteNonQuery()方法或ExecuteReader()方法进行执行数据操作→读取或输入数据至数据库→用Close()方法关闭连接.
由此可知,在使用存储过程前,我们要用SqlCommand对象或SqlDataAdapter对象使填充DataSet或共它在运用存储过程中有很大的作用.但其运用的方法是跟在Net中直接执行Sql语句区别并不是很大的,我们可以通过例子来说明是乍样调用存储过程的.
(1) 采用SqlCommand对象
SqlConnection conndb=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
conndb.Open();
SqlCommand strselect = new SqlCommand("supplyinfo_select_supplyid",conndb);
strselect.CommandType= CommandType.StoredProcedure;
strselect.Parameters.Add("@supply_ID",spid);
SqlDataReader reader = strselect.ExecuteReader();
if(reader.Read())
...{
LblId.Text=reader["Supply_Id"].ToString().Trim();
LblTitle.Text=reader["Supply_Subject"].ToString().Trim();
LblBigclass.Text=reader["Supply_CatID"].ToString().Trim();
LblDesc.Text=reader["Supply_Details"].ToString().Trim();
LblPurType.Text=reader["Supply_PurchaseType"].ToString().Trim();
if(int.Parse(reader["Supply_Ischecked"].ToString().Trim())==1)
...{
LblIschk.Text="已通过审核";
}
else
...{
LblIschk.Text="没有通过审核";
}
if(int.Parse(reader["Supply_Isrcmd"].ToString().Trim())==1)
...{
LblIsrcmd.Text="已设置为推荐";
}
else
...{
LblIsrcmd.Text="没有设置为推荐";
}
switch(reader["Supply_Reader_Level"].ToString().Trim())
...{
case "0":
LblLevel.Text="设置所有人都可以看到此信息";
break;
case "1":
LblLevel.Text="设置注册会员可以看到此信息";
break;
case "2":
LblLevel.Text="设置VIP会员可以看到此信息";
break;
}
}
由上可以看到,利用SqlCommand对象调用存储过程的关键语句是:
strselect.CommandType= CommandType.StoredProcedure;
strselect.Parameters.Add("@supply_ID",spid);
简单解释:声明一个SqlCommand对像,通过SqlCommand调用存储过程supplyinfo_select_supplyid,
同时包含了一个输入参数@supply_id,其值是变量spid,同时通过ExecuteReader()方法,查询数据相关的数据,通过label控件,将数据显示出来.
(2)采用SqlDataAdapter对象
程序代码:
{
SqlConnection conndb= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
conndb.Open();
SqlDataAdapter strselect = new SqlDataAdapter("productclass",conndb);
strselect.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
strselect.Fill(ds);
DlstBuycatalog.DataSource =ds;
DlstBuycatalog.DataKeyField ="PdtCat_ID";
DlstBuycatalog.DataBind();
conndb.Close();
}
以上这个方法,就是通过SqlDataAdapter对像调用了SQL中存储过程productclass,通过DataSet将数据填充在ds中,同时指定DataList控件DlstBuycatalog的数据源是ds,主键是PdtCat_Id,最后再重新绑定Datalist控件.由这个方法我们可以看到用SqlDataAdapter调用存储过程中的关键是:
SqlDataAdapter strselect = new SqlDataAdapter("productclass",conndb);
strselect.SelectCommand.CommandType = CommandType.StoredProcedure;
当存储过程中有参数时,我们又应该乍样做呢?其实这个跟SqlCommand的差不多,我们只要再加一句
Strselect.SelectCommand.Parameter.Add(“@pdt_name”,txtpdtname.Text());
就可以了,其中@pdt_name是在存储过程中声明的参数变量名,而txtpdtname.text()是在.net中赋于变量@pdt_name的值了。认真看一下下面这个存储过程就很清楚了:
由上面我们可以知道在调用存储过程中,最关键的对象是Command对象,这个对象可以通过ExecuteReader()方法执行数据查询,还可以返回一个单一值的查询,还可以通过ExecuteScalar()方法进行相关的数据统计,还可以通过ExecuteNonQuery()方法进行数据更新,增删改的执行操作,而在执行这些SQL操作时,往往是与相关的控件DataGrid ,DataList,Repeat控件结合使用的.