处理并发操作的简单方法

Posted on 2005-02-28 12:08  恐龙的绿色森林  阅读(1858)  评论(10)    收藏  举报

编程的时候常遇到的一个问题是当A在操作的时候,B进来修改了数据,A保存的时候用旧的数据覆盖了新的数据。下面是一个简单的ASP.NET例子,C#代码,实现的流程比较简单,如下

程序有2个文件,view.aspx 和 edit.aspx,浏览在view.aspx,修改在edit.aspx,当操作人员修改的时候,会首先查询修改标志位,如果为1,说明在有人修改,提示等待,如果为0,说明可以修改,允许进入edit.aspx页面修改,代码如下:


view.aspx
.cs

namespace Concurrent
{
    
/// <summary>
    
/// Summary description for home.
    
/// </summary>

    public class home : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Label Label1;
        
protected System.Web.UI.WebControls.Label Label2;
        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
    

        
        
Web Form Designer generated code



    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
if (!(Page.IsPostBack ))
            
            
{
                
                DataFiller1();
            
                
            }
// end if

        
        }
  //End Page_load


        
void DataFiller1()
        
{
        
            
//在view.aspx中显示数据
            String strConnection = "workstation id=localhost;packet size=4096;user id=user1;initial catalog=Concurrent;persist security info=True;password=123";
            SqlConnection objConnection 
= new SqlConnection(strConnection);
            String strSQL
= "SELECT * FROM Concurrent ";
            SqlDataAdapter objDataAdapter 
= new SqlDataAdapter(strSQL, objConnection);
            DataSet objDataSet 
= new DataSet();
            objDataAdapter.Fill(objDataSet, 
"Concurrent");
            DataView objDataView
= new DataView(objDataSet.Tables["Concurrent"]);
     
            objConnection.Open();
            DataGrid1.DataSource 
= objDataView;
            DataGrid1.DataBind();
            objConnection.Close();
        }

    
        
//判断是否能修改的代码
        public void Click_Grid(Object sender, DataGridCommandEventArgs e)

        
{
            
if (((LinkButton)e.CommandSource).CommandName=="Select")

            
{
                
int NewsID = Convert.ToInt32(e.Item.Cells[0].Text);
                String strConnection 
= "workstation id=localhost;packet size=4096;user id=user1;initial catalog=Concurrent;persist security info=True;password=123";
                SqlConnection objConnection 
= new SqlConnection(strConnection);
                String strSQL
= "SELECT editing FROM Concurrent where id = '" + NewsID + "'";
                SqlDataAdapter objDataAdapter 
= new SqlDataAdapter(strSQL, objConnection);
                DataSet objDataSet 
= new DataSet();
                objDataAdapter.Fill(objDataSet, 
"Concurrent");
                
                DataTable while_edit 
= objDataSet.Tables["Concurrent"];

               
//取出并发标志位,为1不能修改,为零可以修改,送出验证标志
                string edit_id =Convert.ToString(while_edit.Rows[0][0]); 
                
                
if  (Convert.ToInt32(edit_id) ==0
   
                
{
            
                    
                    
 //送出待验证标志,等待edit.aspx页面验证

                                
                }

                
                
else

                
{
              
                    Label2.Text 
= "编辑中,请等待";
                
                }


                
            }


        }

        
    
    
    
    }
//End Class

}
//End namespace


这里没有给出如何校验验证标志的方法,有兴趣的读者可以自行尝试。

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3