if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SystemKey]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[SystemKey]
GO
CREATE TABLE [dbo].[SystemKey] (
[ID] [int] NOT NULL ,
[KeyName] [nvarchar] (50) NOT NULL ,
[KeyValue] [int] NOT NULL ,
[SourceID] [nvarchar] (50) NOT NULL ,
[LockTime] [datetime] NULL
) ON [PRIMARY]
GO
Imports Microsoft.ApplicationBlocks.Data
Imports Microsoft.VisualBasic.CompilerServices
Imports System.Threading
Imports System.Data.SqlClient
Public Class ClassTestClass ClassTest
Public Function GetKeys()Function GetKeys(ByVal KeyName As String, ByVal Source As String, ByVal CNString As String) As Integer
Dim connection As New SqlConnection(CNString)
Dim NewNum As Integer
Dim obj2 As Object
Dim sFlage As String = "Flag"
Try
Dim sql As String
Dim time As DateTime = DateAndTime.Now.AddSeconds(1)
connection.Open()
Do While (StringType.StrCmp(sFlage, "", False) 0)
sql = (("Update [SystemKey] Set [SourceID]='" & Source & "', [LockTime]=GetDate() Where [KeyName]='" & KeyName) & "' AND ((DATEADD(millisecond, 1000, LockTime) 0) Then
sFlage = ""
Exit Do
End If
sFlage = "Err"
connection.Close()
If (DateTime.Compare(time, DateAndTime.Now) < 0) Then
Return -1
End If
Thread.Sleep(10)
Loop
sql = "Select KeyValue From [SystemKey] Where [KeyName]='" & KeyName & "' AND SourceID='" & Source & "'"
Dim OldNum As Object = SqlHelper.ExecuteScalar(connection, CommandType.Text, sql)
Dim num As Integer = (IntegerType.FromObject(OldNum) + 1)
sql = "Update [SystemKey] Set [KeyValue]=" & StringType.FromInteger(num) & ", [SourceID]='' Where [KeyName]='" & KeyName & "'"
SqlHelper.ExecuteNonQuery(connection, CommandType.Text, sql)
NewNum = num
Catch exception As Exception
NewNum = -1
Finally
If Not connection Is Nothing Then
CType(connection, IDisposable).Dispose()
End If
End Try
Return NewNum
End Function
End Class