现在的位置: 首页 > 综合 > 正文

如何实现对SQL数据表加锁在C#中webform实现

2014年02月13日 ⁄ 综合 ⁄ 共 2465字 ⁄ 字号 评论关闭
 

  using   System;   
  
using   System.Collections;   
  
using   System.ComponentModel;   
  
using   System.Data;   
  
using   System.Drawing;   
  
using   System.Web;   
  
using   System.Web.SessionState;   
  
using   System.Web.UI;   
  
using   System.Web.UI.WebControls;   
  
using   System.Web.UI.HtmlControls;   
  
using   System.Data.SqlClient;   
    
    
  
namespace   WebApplication8   
  {   
  
///   <summary>   
  
///   WebForm1   的摘要说明。   
  
///   </summary>   
  public   class   WebForm1   :   System.Web.UI.Page   
  {   
  
private   SqlTransaction   cmicTr=null;   
  
private   SqlConnection   cmicCn=new   SqlConnection("server=(local);uid=sa;pwd=kjxy.net;database=exam_paper;");   
  
private   SqlCommand   cmicCmd=new   SqlCommand();   
            
  
protected   System.Web.UI.WebControls.Button   Button1;   
  
protected   System.Web.UI.WebControls.Button   Button3;   
  
protected   System.Web.UI.WebControls.TextBox   TextBox1;   
    
  
private   void   Page_Load(object   sender,   System.EventArgs   e)   
  {   
  cmicCn.Open();   
    
    
  }   
    
  
#region   Web   Form   Designer   generated   code   
  
override   protected   void   OnInit(EventArgs   e)   
  {   
  
//   
  
//   CODEGEN:该调用是   ASP.NET   Web   窗体设计器所必需的。   
  
//   
  InitializeComponent();   
  
base.OnInit(e);   
  }   
    
  
///   <summary>   
  
///   设计器支持所需的方法   -   不要使用代码编辑器修改   
  
///   此方法的内容。   
  
///   </summary>   
  private   void   InitializeComponent()   
  {           
  
this.Button1.Click   +=   new   System.EventHandler(this.Button1_Click);   
  
this.Button3.Click   +=   new   System.EventHandler(this.Button3_Click);   
  
this.Load   +=   new   System.EventHandler(this.Page_Load);   
    
  }   
  
#endregion   
    
  
private   void   Button1_Click(object   sender,   System.EventArgs   e)   
  {   
  TextBox1.Text
="已加锁";   
    
  
//SqlTransaction   cmicTr=null;   
  try   
  {   
  cmicTr
=cmicCn.BeginTransaction();   
  cmicCmd.Connection
=cmicCn;   
          cmicCmd.Transaction
=cmicTr;   
  cmicCmd.CommandText
="select   *   from   listen   with   (xLOCK)   where   test_id=1";   
    
  cmicCmd.ExecuteNonQuery();
-------------------加锁能够成功   
    
  Session.Add(
"tr",cmicTr);///////   
  }   
  
catch   
  {cmicTr.Rollback();   
    
  }   
  }   
    
  
private   void   Button3_Click(object   sender,   System.EventArgs   e)   
  {   
  TextBox1.Text
="已解锁";   
    
  SqlTransaction   gggTr
=(SqlTransaction)Session["tr"];////////   
  gggTr.Commit();///////////   
  }   
  有什么问题再给我发邮件   
    
    
    
  }   
  }   

抱歉!评论已关闭.