時間:2015/6/28來源:IT貓撲網(wǎng)作者:網(wǎng)管聯(lián)盟我要評論(0)
存儲過程的定義:
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表中有沒有指定的商品,有的話則增加他的數(shù)目,沒有的話新添加記錄。
這是在ASP.NET中的調(diào)用過程:
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
關鍵詞標簽:asp.net,存儲過程
相關閱讀
熱門文章 手把手教你用好LINQ to SQL在.NET環(huán)境下為網(wǎng)站增加IP過濾功能ASP.NET 如何避免頁面重新整理時重復送出用Asp.net擴展ExtJS
人氣排行 asp.net表單提交方法GET\POST在ASP.NET中如何判斷用戶IE瀏覽器的版本Asp.net中messagebox的實現(xiàn)方法Asp.net中的web.config配置在ASP.NET MVC中實現(xiàn)大文件異步上傳用Iformattable接口控制.Net中文本格式c#.Net經(jīng)典面試題目用Asp.net擴展ExtJS