学习在asp.net中使用存储过程
2010-08-05 16:14
387 查看
存储过程的定义:
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
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
相关文章推荐
- ASP.NET中 存储过程-Procedure 使用 学习总结
- 【原创】Asp.Net MVC 学习笔记之-使用Model验证
- ASP.net中的AJAX学习记录之三 体会UpdateMode属性及使用UpdateTriggers
- (asp.net MVC学习)System.Web.Mvc.HtmlHelper学习及使用
- 学习asp.net中使用 log4net 笔记
- asp.net下Repeater使用 AspNetPager分页控件 不用存储过程 完美实现
- 在asp.net中使用存储过程
- ASP.NET MVC 学习心得 (3) – 怎样使用服务器控件
- asp.net使用存储过程
- 一步步学习SPD2010--第十四章节--在Web页面使用控件(4)--使用ASP.NET验证控件
- 学习笔记--asp.net母版页(转自msdn,仅为自己学习存储和有意读者使用)
- ASP.net中的AJAX学习记录之三 体会UpdateMode属性及使用UpdateTriggers
- 一起学ASP.NET中如何使用存储过程
- ASP.NET EF 使用LinqPad 快速学习Linq
- asp.net Code学习二(使用vs 2015 update 3)
- asp.net 2.0中使用存储过程
- ASP.NET 3.5核心编程学习笔记(23):Linq-to-SQL 数据的更新、事务、存储过程、函数
- ASP.NET学习笔记[1] - iis不能使用的问题解决
- asp.net调用mysql 存储过程 带 out 返回值,返回刚插入数据库中的自增的ID,LAST_INSERT_ID() 的使用
- ASP.NET EF 使用LinqPad 快速学习Linq