sql中更新列的限制

时间:2014-09-23 07:59:46

标签: sql vb.net

我有一个方案来更新Employee表中的Employee详细信息。

员工

EmpId  EmpName
---------------    
  1    james
  2    Anil
  3    Nandy

多个用户将使用EmpName

更新EmpId

如果一位用户将EmpnameNandy更新为Peter

员工

EmpId  EmpName
----------------    
  1    james
  2    Anil
  3    Peter

在这种情况下,我必须限制另一个用户更新相同的EmpName 60秒。

如果有任何用户将EmpName Peter更新为John,那么我必须显示警告消息为请在60秒后更新。我不知道如何在我的应用程序中实现这一点。

更新

Private Function Getrecentaccountdatevalues() As DataTable
            Dim dsMaxdates As New DataSet
            Dim dtMaxdates As New DataTable
            Dim daMaxdates As SqlDataAdapter
            Try
                Dim strConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection(strConnect(Me))
                Dim ocmdAofA As SqlClient.SqlCommand = New SqlClient.SqlCommand
                ocmdAofA.Connection = strConnection
                ocmdAofA.CommandText = "usp_getMaxdatesAofA"
                ocmdAofA.CommandType = CommandType.StoredProcedure
                ocmdAofA.Parameters.Clear()
                ocmdAofA.Parameters.Add("@acc_AccountId", SqlDbType.Int, 15).Value = Request.QueryString("acct_AccountID")
                ocmdAofA.Parameters.Add("@client_ClientId", SqlDbType.Int, 15).Value = Request.QueryString("acct_ClientID")
                strConnection.Open()
                daMaxdates = New SqlDataAdapter(ocmdAofA)
                daMaxdates.Fill(dtMaxdates)
                strConnection.Close()
                strConnection.Dispose()
                ocmdAofA.Dispose()
            Catch ex As Exception
                Throw ex
            End Try
            Return dtMaxdates
        End Function

在更新结果时比较时间。

Private Sub AofA_Update_Insert()
                Try

                    Dim accountNumber As String = Request.QueryString("acct_AccountID")
                    datatablerecentaccountvalues = Getrecentaccountdatevalues()
                    Dim dr As DataRow = datatablerecentaccountvalues.Rows(0)
                    Dim MaxUpdatedPrepped As DateTime = dr.Item("MaxUpdatedPrepped").ToString()
                    Dim Universaldatetime As DateTime = Now.ToUniversalTime()
                    Dim result As Integer = DateDiff(DateInterval.Second, MaxUpdatedPrepped, Universaldatetime)
                    If (result < 5 And accountNumber = GetrecentAccountID()) Then
                        ASPNET_MsgBox("Due to handling multiple user updates on same account at same time.Please Reset the page.")
                        Exit Sub
                    End If
End Try

2 个答案:

答案 0 :(得分:1)

为什么不添加TimeStamp列,比如LastUpdate,并在更新查询中检查新更新是否在范围内?

<强> [更新]

IF (SELECT DATEDIFF(SECOND,LastUpdate,GETDATE())
FROM Table WHERE ID = @Id ) >= 60
BEGIN
    UPDATE Table ...
END

答案 1 :(得分:0)

从您的评论&#34;但是有没有选项来限制更新操作而不添加任何列&#34;我只能假设您不允许触摸数据库?您可以使用tbl_LastUpdateIDEmpId列添加一个全新的表LastUpdate,并将其用作要检查的链接表吗?

但是,如果您无法更改数据库,则根据应用程序负载,您可以使用存储在dictionary对象中的application对象:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)    
        Application("lastUpdate") = new Dictionary(of int, DateTime)
End Sub

(原谅我的VB,它有点生锈!你可能需要检查)

然后进行更新时:

Sub updateEmpName(EmpId as Integer, EmpName as String)
     ' Get the Dictionary object
     Dim d as Dictionary
     d = Application("lastUpdate")

     if d.containsKey(EmpId) Then
         ' Check if the last update is greater than 60 seconds
         Dim dt as DateTime = d.Item(EmpId)
         if dt.AddSeconds(60) > DateTime.Now Then
            ' Do the update as greater then 60 seconds
            d.Item(EmpId) = DateTime.Now ' Set the datetime to now for this update
         else
            ' No update allowed
         End If
     else
        ' Update allowed as no key present
        d.Add(EmpId, DateTime.Now) ' Add the current ID and Timestamp to check
     End If

     ' Now loop through and clean up all old items in the dictionary to stop it getting to big
     For Each pair in d
         if pair.Value.AddSeconds(60) > DateTime.Now Then
              d.Remove(pair.key)
         End if
     Next        


     Application("lastUpdate") = d
End Sub

这里有对词典对象的良好引用:http://www.dotnetperls.com/dictionary-vbnet

如上所述,我的VB非常生锈,所以代码需要进行三重检查,但希望它足以帮助您入门!