标题:处理并发操作的简单方法(.net编程)

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

为了简化范例,程序只有1个主要文件,edit.aspx.cs,浏览在和修改都在edit.aspx,当操作人员修改的时候,会直接进入修改状态,提交更新的时候会检测是否有人修改过数据,如果修改过,更新不提交,提示刷新。如果未修改过,说明可以更新,把更新值加1后存入表的检测更新字段,更新提交,流程图如下:


界面


表的字段定义·


edit.aspx HTML主要代码

<asp:datagrid id="DataGrid1" style="Z-INDEX: 106; LEFT: 304px; POSITION: absolute; TOP: 88px"
runat
="server" BorderColor="Tan" BackColor="LightGoldenrodYellow" Width="272px" ForeColor="Black" OnItemCommand="Click_Grid" AutoGenerateColumns="False" GridLines="None" CellPadding="2" BorderWidth="1px">
<SelectedItemStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedItemStyle>
<AlternatingItemStyle BackColor="PaleGoldenrod"></AlternatingItemStyle>
<HeaderStyle Font-Bold="True" BackColor="Tan"></HeaderStyle>
<FooterStyle BackColor="Tan"></FooterStyle>
<Columns>
<asp:BoundColumn Visible="False" DataField="id" SortExpression="id"></asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="edit_series" SortExpression="id"></asp:BoundColumn>
<asp:BoundColumn DataField="title" SortExpression="id" HeaderText="标题"></asp:BoundColumn>
<asp:BoundColumn DataField="content" SortExpression="id" HeaderText="内容"></asp:BoundColumn>
<asp:ButtonColumn Text="修改" HeaderText="修改" CommandName="Select"></asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="DarkSlateBlue" BackColor="PaleGoldenrod"></PagerStyle>
</asp:datagrid>


edit.aspx.cs代码如下:

  

 

        private void Page_Load(object sender, System.EventArgs e)
        
{
            
// Put user code to initialize the page here

            
if (!(Page.IsPostBack ))
            
            
{
                DataFiller1();
            }

          }



       
Web Form Designer generated code

        
        
void DataFiller1()
        
{
        
            
//在datagrid中浏览数据
            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")

            
{
                
//利用数据表中第一项ID(不可见Label)排序,并作为条件取出数据
                IDLabel.Text = e.Item.Cells[0].Text;
                
//将修改序列号存入不可见Label
                editIDLabel.Text = e.Item.Cells[1].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 title,content FROM Concurrent where id = '" + IDLabel.Text + "'";
                SqlDataAdapter objDataAdapter 
= new SqlDataAdapter(strSQL, objConnection);
                DataSet objDataSet 
= new DataSet();
                objDataAdapter.Fill(objDataSet, 
"Concurrent");
                DataTable while_edit 
= objDataSet.Tables["Concurrent"];
                
                
//取出字段,填充要更新栏目
                titleBox.Text  = Convert.ToString(while_edit.Rows[0][0]); 
                contentBox.Text
= Convert.ToString(while_edit.Rows[0][1]);
                
            }

                
        }



        
private void UpdButton_Click(object sender, System.EventArgs e)
        
{
            
            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 edit_series FROM Concurrent where id = '" + IDLabel.Text + "'";
            SqlDataAdapter objDataAdapter 
= new SqlDataAdapter(strSQL, objConnection);
            DataSet objDataSet 
= new DataSet();
            objDataAdapter.Fill(objDataSet, 
"Concurrent");
            DataTable while_edit 
= objDataSet.Tables["Concurrent"];

            
//取出修改序列号字段,看数值和原先比较是否有变化,
            
//假如相同,可以更新,把修改序列号+1后和数据一起更新。
            
//假如不等,提示数据已经更改,需重新刷新页面
            if (Convert.ToInt32(editIDLabel.Text) == Convert.ToInt32(while_edit.Rows[0][0])) 
            
{
                
int editID =Convert.ToInt32(editIDLabel.Text) +1;
                
string UpdateSQL  = "update Concurrent set title='" + titleBox.Text + "',content = '"  + contentBox.Text + "',edit_series= '" + editID + "' where id='" + IDLabel.Text + "'";
                SqlCommand Sqlupdate 
= new SqlCommand(UpdateSQL, objConnection);
         
                objConnection.Open();
                Sqlupdate.ExecuteNonQuery();
                objConnection.Close(); 
                
                DataFiller1();
                editIDLabel.Text
=Convert.ToString(editID);
                
//message.Text =  Convert.ToString(editID);
            }

            
else
            
{
                        DataFiller1();
                          message.Text = "数据已经有人更改,请重新修改!";
                
            }


        }

    
    }
}


附记:
程序现在主要还是靠.net代码实现,为了优化并发操作,检测修改改在提交更新时完成,缺点是如果中途有人修改,新的修改会丢失,要重新输入。
当然可以增加额外的代码解决这一问题。再次多谢小得乐和听棠.net的建议。




 

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