自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

通過程序獲得SQL數(shù)據(jù)庫中的GetKey函數(shù)

數(shù)據(jù)庫 SQL Server
如果需要獲得SQL Server自增型字段的GetKey函數(shù),可以通過程序獲得,下面就為您介紹該方法,供您參考。

下面將為您介紹通過程序獲得SQL Server自增型字段的函數(shù)--GetKey函數(shù)的方法,供您參考,希望對你更好學習SQL中函數(shù)能夠有所幫助。

概述:

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

關鍵步驟:

1. 創(chuàng)建用于存放需要自增的數(shù)據(jù)表.(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:鎖定的時間,在程序內(nèi)部使用.

2. GetKeys函數(shù)方程,通過調(diào)用GetKeys函數(shù)得到關鍵字的值.

函數(shù)描述如下:

  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函數(shù)的用法

SQL中循環(huán)語句的效果實例

SQL中類似For循環(huán)處理的實例

對存儲過程代替SQL語句的討論

SQL聚合函數(shù)之Avg 函數(shù)

責任編輯:段燃 來源: 互聯(lián)網(wǎng)
相關推薦

2010-09-10 16:12:08

sql函數(shù)判斷

2011-05-30 14:30:08

函數(shù)存儲過程

2011-06-03 10:50:27

Java

2010-09-09 14:31:31

SQL函數(shù)數(shù)據(jù)庫

2010-09-06 11:05:05

SQL SERVER語句

2011-08-25 17:15:04

2011-04-13 15:44:12

SQL Server數(shù)函數(shù)

2010-07-22 10:45:45

SQL Server數(shù)

2019-08-19 11:07:41

SQL數(shù)據(jù)庫優(yōu)化

2019-10-21 08:08:34

MySQL數(shù)據(jù)庫主鍵

2011-07-05 16:27:14

過程函數(shù)PL

2011-08-22 13:04:47

SQL Server數(shù)函數(shù)

2011-08-18 10:36:24

SQL ServerISNULL函數(shù)

2011-08-22 11:39:53

SQL Server數(shù)PIVOT

2010-09-02 11:24:45

SQL刪除

2011-03-22 10:44:20

SQL Server數(shù)拆分字符串函數(shù)

2010-06-17 13:34:47

SQL Server數(shù)

2010-07-12 15:49:53

MS SQL Serv

2009-09-11 15:12:26

LINQ執(zhí)行存儲過程

2010-09-10 13:50:51

SQLCOUNT函數(shù)
點贊
收藏

51CTO技術(shù)棧公眾號