如何在ms访问中以编程方式创建GUID自动编号

时间:2010-08-26 08:02:22

标签: c# ms-access

我正在尝试以编程方式创建guid自动编号字段,但我不能。请帮帮我。 我正在使用c#。

4 个答案:

答案 0 :(得分:2)

为了使您的GUID字段自动递增,请使用GenGUID()作为其默认值。

这适用于使用ADO的Access。也许类似的陈述也适用于C#:

CurrentProject.Connection.Execute "CREATE TABLE hede (Id Guid DEFAULT GenGUID())"

答案 1 :(得分:2)

如果是C#那么

string myguid = Guid.NewGuid.ToString;

如果是Access,那么

Private Declare Function CreateGuid Lib "OLE32.DLL" (pGuid As GUID) As Long
Private Declare Function StringFromGUID2 Lib "OLE32.DLL" (pGuid As GUID, ByVal PointerToString As Long, ByVal MaxLength As Long) As Long

Private Type GUID
    Guid1           As Long
    Guid2           As Integer
    Guid3           As Integer
    Guid4(0 To 7)   As Byte
End Type

Public Function CreateGUIDKey() As String
    Const GUID_OK As Long = 0    
    Const GUID_LENGTH As Long = 38

    Dim udtGUID As GUID
    Dim FormattedGUID As String
    Dim Result As Long

    Result = CreateGuid(udtGUID)

    If Result = GUID_OK Then
        FormattedGUID = String$(GUID_LENGTH, 0)
        StringFromGUID2 udtGUID, StrPtr(FormattedGUID), GUID_LENGTH + 1
    Else
        FormattedGUID = ""
    End If

    CreateGUIDKey = FormattedGUID

End Function

答案 2 :(得分:1)

答案 3 :(得分:0)

你知道如何针对Jet或ACE连接运行sql吗?

CREATE TABLE Literal (LinkID GUID)

INSERT INTO Literal (LinkID) VALUES ({guid {11223344-1122-1122-1122-AABBCCDDEEFF}})
相关问题