日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

通过程序获得SQL Server自增型字段的函数:GetKey

發(fā)布時間:2025/4/14 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过程序获得SQL Server自增型字段的函数:GetKey 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

通過程序獲得SQL Server自增型字段的函數(shù):GetKey

?

概述:

通過程序來產(chǎn)生自增型字段,可以避免多用戶操作的讀取臟數(shù)據(jù),操作也很簡便.可以更好的在程序中控制這些關鍵字段的數(shù)值.

?

關鍵步驟:

1.???? 創(chuàng)建用于存放需要自增的數(shù)據(jù)表.(systemkey)

SQL Script 如下:

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

KeyName:關鍵字的字段名(我們需要的字段名稱,手工添加到這個表中)

KeyValue:對應字段名的值.

SourceID:字段的來源,如果沒有可以填””

LockTime:鎖定的時間,在程序內(nèi)部使用.

?

2.??? GetKeys方程,通過調用GetKeys得到關鍵字的值.

函數(shù)描述如下:

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)?<GetDate()?)?OR?(?SourceID=''))")
????????????????
Dim?j?As?Integer?=?SqlHelper.ExecuteNonQuery(connection,?CommandType.Text,?sql)
????????????????
If?(j?>?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

轉載于:https://www.cnblogs.com/rippleyong/archive/2005/01/31/99892.html

總結

以上是生活随笔為你收集整理的通过程序获得SQL Server自增型字段的函数:GetKey的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。