IT猫扑网:您身边最放心的安全下载站! 最新更新| 软件分类| 专题汇总| 手机版

您当前所在位置:IT猫扑网 > 网络编程 > .Net编程 > 在asp.net中使用存储过程

在asp.net中使用存储过程

时间:2015-06-28 00:00 来源:IT猫扑网|http://www.itmop.com/ 作者:网管联盟 我要评论(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表中有没有指定的商品,有的话则增加他的数目,没有的话新添加记录。

  这是在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,存储过程

相关阅读 ASP.NET创建XML Web服务全接触 如何提高ASP.NET页面载入速度的方法 .net导出海量数据到execl文件 ASP.NET 2.0程序安全的基础知识 ASP.NET中MD5与SHA1加密的几种方法 在.net开发中几个重要的认识误区

文章评论
发表评论

热门文章 在ASP.NET MVC中实现大文件异步上传 在ASP.NET MVC中实现大文件异步上传 在.NET环境下为网站增加IP过滤功能 在.NET环境下为网站增加IP过滤功能 诛仙3飞升任务怎么做-诛仙3飞升任务流程最新2022 诛仙3飞升任务怎么做-诛仙3飞升任务流程最新2022 ASP.NET 如何避免页面重新整理时重复送出 ASP.NET 如何避免页面重新整理时重复送出 钟离圣遗物推荐-原神钟离圣遗物词条 钟离圣遗物推荐-原神钟离圣遗物词条

相关下载

人气排行 诛仙3飞升任务怎么做-诛仙3飞升任务流程最新2022 asp.net表单提交方法GETPOST 在ASP.NET中如何判断用户IE浏览器的版本 Asp.net中messagebox的实现方法 Asp.net中的web.config配置 在ASP.NET MVC中实现大文件异步上传 asp.net获取URL和IP地址 FileUpload上传多文件出现错误的解决方法 ASP.NET Web.config配置详解 Asp.net常用的51个代码(非常实用) ASP.NET打开word文档出错的解决办法 让你的.NET程序兼容不同版本的Dll文件