通过程序获得SQL数据库中的GetKey函数

数据库 SQL Server
如果需要获得SQL Server自增型字段的GetKey函数,可以通过程序获得,下面就为您介绍该方法,供您参考。

下面将为您介绍通过程序获得SQL Server自增型字段的函数--GetKey函数的方法,供您参考,希望对你更好学习SQL中函数能够有所帮助。

概述:

通过程序来产生自增型字段,可以避免多用户操作的读取脏数据,操作也很简便.可以更好的在程序中控制这些关键字段的数值.

关键步骤:

1. 创建用于存放需要自增的数据表.(systemkey)

SQL Script 如下:

  1. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SystemKey]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)   
  2. drop table [dbo].[SystemKey]   
  3. GO   
  4.    
  5. CREATE TABLE [dbo].[SystemKey] (   
  6.     [ID] [int] NOT NULL ,   
  7.     [KeyName] [nvarchar] (50)  NOT NULL ,   
  8.     [KeyValue] [int] NOT NULL ,   
  9.     [SourceID] [nvarchar] (50)  NOT NULL ,   
  10.     [LockTime] [datetime] NULL    
  11. ) ON [PRIMARY]   
  12. GO   
  13.   

 KeyName:关键字的字段名(我们需要的字段名称,手工添加到这个表中)

KeyValue:对应字段名的值.

SourceID:字段的来源,如果没有可以填””

LockTime:锁定的时间,在程序内部使用.

2. GetKeys函数方程,通过调用GetKeys函数得到关键字的值.

函数描述如下:

  1. Imports Microsoft.ApplicationBlocks.Data   
  2. Imports Microsoft.VisualBasic.CompilerServices   
  3. Imports System.Threading   
  4. Imports System.Data.SqlClient   
  5. Public Class ClassTestClass ClassTest   
  6.     Public Function GetKeys()Function GetKeys(ByVal KeyName As String, ByVal Source As String, ByVal CNString As String) As Integer   
  7.         Dim connection As New SqlConnection(CNString)   
  8.    
  9.         Dim NewNum As Integer   
  10.         Dim obj2 As Object   
  11.         Dim sFlage As String = "Flag"   
  12.         Try   
  13.             Dim sql As String   
  14.             Dim time As DateTime = DateAndTime.Now.AddSeconds(1)   
  15.             connection.Open()   
  16.             Do While (StringType.StrCmp(sFlage, "", False) <> 0)   
  17.                 sql = (("Update [SystemKey] Set [SourceID]='" & Source & "', [LockTime]=GetDate()  Where [KeyName]='" & KeyName) & "' AND   ((DATEADD(millisecond, 1000, LockTime) <GetDate() ) OR ( SourceID=''))")   
  18.                 Dim j As Integer = SqlHelper.ExecuteNonQuery(connection, CommandType.Text, sql)   
  19.                 If (j > 0) Then   
  20.                     sFlage = ""   
  21.                     Exit Do   
  22.                 End If   
  23.                 sFlage = "Err"   
  24.                 connection.Close()   
  25.                 If (DateTime.Compare(time, DateAndTime.Now) < 0) Then   
  26.                     Return -1   
  27.                 End If   
  28.                 Thread.Sleep(10)   
  29.             Loop   
  30.    
  31.             sql = "Select KeyValue  From [SystemKey] Where [KeyName]='" & KeyName & "' AND SourceID='" & Source & "'"   
  32.             Dim OldNum As Object = SqlHelper.ExecuteScalar(connection, CommandType.Text, sql)   
  33.             Dim num As Integer = (IntegerType.FromObject(OldNum) + 1)   
  34.             sql = "Update [SystemKey] Set [KeyValue]=" & StringType.FromInteger(num) & ", [SourceID]='' Where [KeyName]='" & KeyName & "'"   
  35.             SqlHelper.ExecuteNonQuery(connection, CommandType.Text, sql)   
  36.             NewNum = num   
  37.         Catch exception As Exception   
  38.             NewNum = -1   
  39.         Finally   
  40.             If Not connection Is Nothing Then   
  41.                 CType(connection, IDisposable).Dispose()   
  42.             End If   
  43.         End Try   
  44.         Return NewNum   
  45.     End Function   
  46. End Class   
  47.   

 

 

 

【编辑推荐】

SQL中DATENAME函数的用法

SQL中循环语句的效果实例

SQL中类似For循环处理的实例

对存储过程代替SQL语句的讨论

SQL聚合函数之Avg 函数

责任编辑:段燃 来源: 互联网
相关推荐

2010-09-10 16:12:08

sql函数判断

2011-05-30 14:30:08

函数存储过程

2010-09-09 14:31:31

SQL函数数据库

2011-06-03 10:50:27

Java

2010-09-06 11:05:05

SQL SERVER语句

2011-04-13 15:44:12

SQL Server数函数

2011-08-25 17:15:04

2010-07-22 10:45:45

SQL Server数

2019-10-21 08:08:34

MySQL数据库主键

2011-07-05 16:27:14

过程函数PL

2019-08-19 11:07:41

SQL数据库优化

2011-08-22 13:04:47

SQL Server数函数

2011-08-18 10:36:24

SQL ServerISNULL函数

2011-08-22 11:39:53

SQL Server数PIVOT

2010-09-02 11:24:45

SQL删除

2011-03-22 10:44:20

SQL Server数拆分字符串函数

2010-06-17 13:34:47

SQL Server数

2010-07-12 15:49:53

MS SQL Serv

2010-09-10 13:50:51

SQLCOUNT函数

2009-09-11 15:12:26

LINQ执行存储过程
点赞
收藏

51CTO技术栈公众号