无为清净楼资源网 Design By www.qnjia.com
Visual Studio.Net为SQL的存储过程提供了强大的支持,您既可以通过visual studio.net来新建存储过程,也可以直接在Sql Server的查询分析器中运行,还可以通过企业管理器创建,使用起来也非常方便。大家一直都误认为SQL存储过程是一个比较“高深”的技术,其实掌握一般的语法是没有什么大问题的,而我们在使用存储教程中也主要是增删减的操作,学会使用一般的T-SQL就很容易上手了。
我们先来看一下在Sql-server中是如何创建一个存储过程的吧,我们可以使用SQL命令语句创建,也可以通过SQL server中的企业管理器来创建,但其实都是离不开自己写语句的,当然系统存储过程我们就不用去动它了(存储过程分为系统存储过程 ,本地存储过程,临时存储过程,远程存储过程,扩展存储过程),而本地存儲過程就是我們自己編寫的存储过程,其实也叫用户存储过程。
当创建存储过程时需要确定存储过程的三个组成部分
"codetitle">复制代码 代码如下:
/*
用途:查询所有的公司名录
德仔创建于2006-3-29
*/
create procedure com_select
as
select * from Company
GO
b. 有参数的存储过程
/*
选择对应的admin
创建者:德仔
创建日期:2006-4-20
*/
create procedure admin_select
@adminusername char(50),
@adminpassword char(50)
as
select * from superadmin where
[Admin_Name]=@adminusername and [Admin_Password]=@adminpassword
GO

c. 在该存储过程中使用了OUTPUT 保留字有返回值的存储过程
create procedure salequa
@stor_id char 4 ,
@sum smallint output
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

上面的几个存储过程是基本的存储过程,同时我们可以看到在存储过程中注释是用/* 注释 */形式.

我们下一次再讲讲存储过程在net中的使用吧
----------------
二、存储过程使用篇
1. 在SQL中执行
执行已创建的存储过程使用EXECUTE 命令其语法如下
[EXECUTE]
{[@return_statur=]
{procedure_name[;number] | @procedure_name_var}
[[@parameter=] {value | @variable [OUTPUT] | [DEFAULT] [,…n]
[WITH RECOMPILE]
各参数的含义如下
"codetitle">复制代码 代码如下:
string spid=Request.QueryString["supplyid"].Trim();
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对象调用存储过程的关键语句是:
SqlCommand strselect = new SqlCommand("supplyinfo_select_supplyid",conndb);
strselect.CommandType= CommandType.StoredProcedure;
strselect.Parameters.Add("@supply_ID",spid);
简单解释:声明一个SqlCommand对像,通过SqlCommand调用存储过程supplyinfo_select_supplyid,
同时包含了一个输入参数@supply_id,其值是变量spid,同时通过ExecuteReader()方法,查询数据相关的数据,通过label控件,将数据显示出来.
(2)采用SqlDataAdapter对象

程序代码:
复制代码 代码如下:
private void buycatalog()
{
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控件结合使用的.
(3)常用的一些存储过程例子
以下是自己在最近所做的一个项目中所用到的一些存储过程,可能由于自己水平有限,有些写得不是很规范,不过大部分都实现到我想要的结果了,这些存储过程都可以正常执行,把这些发出来给大家(数据库因保密请见谅),希望对大家用用,同时希望指正其中的错误,谢谢。
(1) 选择所有的记录
程序代码:
复制代码 代码如下:
/*
作者:德仔
用途:查询sellinfo里所有的记录
日期:2006-3-23
*/
create procedure sellinfo_select
as
select * from sellinfo
GO

(2) 删除指定的ID记录

程序代码:
复制代码 代码如下:
/*
作者:德仔
用途:删除sellinfo里由输入参数@sell_id指定的ID记录
日期:2006-3-23
*/
CREATE PROCEDURE sellinfo_delete
@sell_id bigint
as
delete from [sellinfo]
where
sell_id=@sell_id
GO

(3)更新所对应的记录

程序代码:
复制代码 代码如下:
/*
作者:德仔
用途:修改相对应的小类名
日期:2006-4-5
*/
create procedure prosmallclass_update_id
@smallid int,
@smallname char(50)
as
update [ProductCats]
set
PdtCat_Name = @smallname
where
PdtCat_id =@smallid
GO

(4)验证登陆

程序代码:
复制代码 代码如下:
/*
作者:德仔
用途:通过得到的@user_name @user_password验证登陆
日期:2006-3-21
*/
CREATE procedure user_login
@user_name varchar(50),
@user_password varchar(50)
as
select * from usercompany where [User_Name] = @User_Name and [User_Pwd] = @User_Password
if @@rowcount>0
begin
update [users] set user_LoginTimes=user_LoginTimes+1 where [User_Name] = @User_Name and [User_Pwd] = @User_Password
end
GO

(5)密码修改

程序代码:
复制代码 代码如下:
/*
作者:德仔
用途:先查到user的密码,再修改新密码
日期:2006-3-23
*/
create procedure user_pwd
@user_name varchar(30),
@user_oldpwd varchar(30),
@user_newpwd varchar(30),
@iOutput int output
as
if exists(select * from users where User_Name=@user_name and user_pwd=@user_oldpwd)
begin
update users set user_pwd=@user_newpwd where User_Name=@user_name and user_pwd=@user_oldpwd
set @iOutput = 1
end
else
set @ioutput = -1
GO

(6)增加新记录

程序代码:
复制代码 代码如下:
/*
作者:德仔
用途:添加一条新留言
日期:2006-4-8
*/
CREATE procedure gb_add
@gbusername char(50),
@gbusermemberid char(50),
@gbuseremail char(50),
@gbusersubject char(50),
@gbusercontent char(1500)
as
insert gb
(
gbusername,
gbusermemberid,
gbuseremail,
gbsubject,
gbcontent
)
values
(
@gbusername,
@gbusermemberid,
@gbuseremail,
@gbusersubject,
@gbusercontent
)
GO

(7)统计数据

程序代码: [ 复制代码到剪贴板 ]
复制代码 代码如下:
/*
作者:德仔
用途:用来统计站上所有的信息总数,包括新闻,产品,公司,等的总数
日期:2006-3-23
*/
CREATE procedure datacount
as
declare @MemberCount int
declare @MemberVip int
declare @MemberNorm int
declare @MemberUnchkReg int
declare @MemberLblRegChk int
declare @CompanyCount int
declare @CompanyRcmd int
declare @SellCount int
declare @SellRcmd int
declare @SellUnchk int
declare @SellChk int
declare @CountSupply int
declare @SupplyRcmd int
declare @SupplyUnchk int
declare @SupplyChk int
declare @NewsCount int
declare @NewsRcmd int
declare @NewsClassCount int
declare @SupplyClass int
declare @SellClass int
declare @MsgCount int
declare @ProBigclass int
declare @proSmallclass int
select @MemberCount= count(User_Id)from Users
select @MemberVip=count(User_Id)from Users where User_Level =2
select @MemberNorm=count(User_Id)from Users where User_Level =1
select @MemberUnchkReg=count(user_id) from users where user_IsChecked=0
select @MemberLblRegChk=count(user_id) from users where user_IsChecked=1
select @CompanyCount=count(COM_id) from Company
select @CompanyRcmd=count(COM_id) from Company where COM_IsRcmd=1
select @SellCount =count(Sell_Id) from sellinfo
select @SellRcmd =count(Sell_Id) from sellinfo where Sell_IsRcmd=1
select @SellUnchk =count(Sell_Id) from sellinfo where Sell_Ischecked = 0
select @SellChk =count(Sell_Id) from sellinfo where Sell_Ischecked = 1
select @CountSupply =count(Supply_Id)from supplyInfo
select @SupplyRcmd =count(Supply_Id)from supplyInfo where Supply_Isrcmd=1
select @SupplyUnchk =count(Supply_Id)from supplyInfo where Supply_Ischecked=0
select @SupplyChk =count(Supply_Id)from supplyInfo where Supply_Ischecked=1
select @NewsCount =count(news_id) from news
select @NewsRcmd =count(news_id) from news where News_Recommand=1
select @NewsClassCount =count(news_id) from news
select @proBigclass = count(PdtCat_SortId) from productcats where PdtCat_SortId=0
select @proSmallClass = count(PdtCat_SortId)from productcats where PdtCat_SortId<>0
select @MsgCount = count(Msg_id) from MSg
select
MemberCount=@MemberCount,
MemberVip=@MemberVip,
MemberNorm=@MemberNorm,
MemberUnchkReg=@MemberUnchkReg,
MemberLblRegChk=@MemberLblRegChk,
CompanyCount=@CompanyCount,
CompanyRcmd=@CompanyRcmd,
SellCount=@SellCount,
SellRcmd=@SellRcmd,
SellUnchk=@SellUnchk,
SellChk=@SellChk,
CountSupply =@CountSupply,
SupplyRcmd =@SupplyRcmd,
SupplyUnchk=@SupplyUnchk,
SupplyChk =@SupplyChk,
NewsCount=@NewsCount,
NewsRcmd=@NewsRcmd,
NewsClassCount=@NewsClassCount,
probigclass=@probigclass,
prosmallclass=@prosmallclass,
MsgCount = @MsgCount
GO

(8)模糊查询

程序代码:
复制代码 代码如下:
/*
作者:德仔
用途:用来进行查询sell_info
日期:2006-4-10
*/
CREATE PROCEDURE sellinfo_search
@keyword nvarchar (20)
AS
select sell_subject from sellinfo where sell_subject like '%' + @keyword + '%'
GO

以上只是自己在学习asp.net中的一点个人经验,因个人水平所限,不免有错,欢迎大家指正,并请多多指教!
标签:
sql,存储过程

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com