学习在asp.net中使用存储过程

存储过程的定义: 
CREATE procedure pro_buyGoods 

@GoodsName varchar(30), 
@GoodsNum int 

AS 
declare @count as int 
set @count = (select count(*) from T_Goods where GoodsName=@GoodsName) 
if @count<=0 begin 
insert into T_Goods values (@GoodsName,@GoodsNum) 
end 
else begin 
update T_Goods set GoodsNum=GoodsNum+@GoodsNum where GoodsName=@GoodsName 
end 
GO 
说明:语法是通过检查的,这个过程是用来判断T_Goods表中有没有指定的商品,有的话则增加他的数目,没有的话新添加记录。 

这是在ASP.NET中的调用过程: 
int BuyGoodsID = Convert.ToInt32(this.txtBuyID.Text); 
string GoodsName = this.txtGoodsName.Text; 
int GoodsNum = Convert.ToInt32(this.txtGoodsNum.Text); 
int GoodsPrice = Convert.ToInt32(this.txtGoodsPrice.Text); 

SqlConnection conn = DB.CreateCon(); 
conn.Open(); 

string cmdText = "insert into T_BuyGoods values('"+BuyGoodsID+"','"+GoodsName+"','"+GoodsNum+"','"+GoodsPrice+"')"; 
SqlCommand cmd = new SqlCommand(cmdText,conn); 

SqlTransaction trans = conn.BeginTransaction(); 
cmd.Transaction = trans; 

try 


cmd.ExecuteNonQuery(); 
//插入库存表的代码 

SqlCommand cmd1 = new SqlCommand("pro_buyGoods",conn); 
cmd1.CommandType = CommandType.StoredProcedure; 
cmd1.Parameters.Add("@GoodsName",SqlDbType.VarChar); 
cmd1.Parameters.Add("@GoodsNum",SqlDbType.Int); 

cmd1.Parameters["@GoodsName"].Direction=ParameterDirection.Input; 
cmd1.Parameters["@GoodsNum"].Direction=ParameterDirection.Input; 
cmd1.Parameters["@GoodsName"].Value = GoodsName; 
cmd1.Parameters["@GoodsNum"].Value = GoodsNum; 


cmd1.ExecuteNonQuery(); 


this.Panel1.Visible = false; 
this.Panel2.Visible = true; 
this.lblInfo.Text = "提交成功!"; 
trans.Commit(); 

catch(Exception exp) 

trans.Rollback(); 
Response.Write("<script language='javascript'>alert('提交失败,需要重新提交!')<"+"/script>"); 

finally 

conn.Close(); 

 

***********************************************************************

set @count = (select count(*) from T_Goods where GoodsName=@GoodsName) 
改为
select @count = count(*) from T_Goods where GoodsName=@GoodsName

转载于:https://www.cnblogs.com/yangwt/archive/2010/08/05/1793306.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值