ACCESS中使用GUID全局唯一标识符的自动唯一编号[同步复制ID]之解
背景: 这段时间临时为一个旅游类网站制作一些网站程序。数据表的情况大致如下: 图1 数据库表的大致情况 由于是Access数据库,之前有两个数据表:TC_TourCompany和TC_SubDetail,前者是旅行社名录相关资料(为了方便描述,暂且叫总公司表),后者是下属营业
背景:
这段时间临时为一个旅游类网站制作一些网站程序。数据表的情况大致如下: 图1 数据库表的大致情况 由于是Access数据库,之前有两个数据表:TC_TourCompany和TC_SubDetail,前者是旅行社名录相关资料(为了方便描述,暂且叫“总公司表”),后者是下属营业部(如果有的话)的相关资料(为方便描述,暂且叫“子公司表”)。
由于业务需要,想将之扩展为适用于所有“公司类”(比如酒店、景区、景点、漂流公司、娱乐餐饮、机票代理、交通公司等)的数据表,由于酒店、餐饮娱乐、机票代理等公司都有可能有分部或分公司,所以表的数据结构是差不多的。所以,我们可以通用这样的数据表设计来简化今后的程序开发。当然,我们需要在数据表中新增一列,用于描述公司的类型是旅行社、酒店、景区或是娱乐餐饮类公司等。不在本文的叙述范围,按下不表。
为了方便今后的分类搜索查询,确保公司(包括子公司)的唯一性,所以,我想在上述两个表中增加一列,我把列名叫做GUID。它的每条记录都是唯一不重复的值,类似:{9E4038C8-E965-45B1-BDE1-9F06E6B280A3},这有点象.Net中的System.Guid.NewGuid()生成的值,并用大括号{}包含起来。
做法: 如何在已有数据库表记录的情况下自动生成每一条记录的这些值呢?
一开始,我走了点弯路。在新增GUID列时,我选择了此列的数据类型为“数字”并在下面常规选项卡中“字段大小”中选择了“同步复制 ID”,索引中选择了“有(无重复)”。本以为这样保存结构之后就万事大吉,最终打开表的所有记录时发现,GUID列完全为空,没有任何值!于是,我想了一些办法去插入GUID唯一值。方案之一是在ACCESS中使用SQL语句更新,后来发现此路不通。方案之二就是使用ADO.net编程方式更新表记录,工作量也不小。
有没有更好的办法呢?一个偶尔的想法让我找到了更快更好的解决办法,那就是在设计视图中建立GUID列时,数据类型选择自动编号而不是数字!同时,在下面常规选项卡中“字段大小”中选择了“同步复制 ID”,索引中选择了“有(无重复)”。
如下图: 图2 给总公司名录表(TC_TourCompany表)增加GUID列
图3 给总公司表(TC_TourCompany表)增加GUID列后自动生成GUID记录值
图4 给分公司(分部)TC_SubDetail表增加GUID列
图5 给分公司(分部)TC_SubDetail表增加GUID列后自动生成GUID记录值
以后新增记录时会发生什么?经测试发现,ACCESS会自动搞定生成GUID记录值的问题。OK,完美!
更多的话: 从 Access 生成 SQL 语句时,遇到了 Guid 查询的问题,在 SQL Server 中使用的字符串形式,不能查询出任何数据。
SELECT * FROM tableName WHERE [GUID]='12345678-90AB-CDEF-1234-567890ABCDEF'
如果条件字符串所引用的列为 GUID 类型,那么该条件表达式使用的语法稍微有所不同: WHERE [GUID] = {GUID {12345678-90AB-CDEF-1234-567890ABCDEF}} 请确保包含如上所示的嵌套大括号和连字号。 需要注意的是,嵌入大括号的方法只用于 Where 语句,在 Insert 语句中还是要使用单引号,否则将产生 MALFORMED GUID in query 的错误。
更多参考: ASP.NET开发经验(3) --- 使用 GUID 值来作为数据库行标识 http://blog.joycode.com/moslem/archive/2004/03/23/16930.aspx
其他: 导出/打印Access数据库的结构 http://blog.csdn.net/johnsuna/archive/2008/05/05/2393664.aspx
附录:
Access数据类型与.net OleDbType枚举类型的对应
最常见的数据类型映射列表
访问类型名称 | 数据库数据类型 | OLEDB 类型 | .NET 框架类型 | 成员名称 |
文本 | VarWChar | DBTYPE _ WSTR | System.String | OleDbType.VarWChar |
备忘录 | LongVarWCha R | DBTYPE _ WSTR | System.String | OleDbType.LongVarWChar |
字节数: | UnsignedTinyInt | DBTYPE _ UI 1 | System.Byte | OleDbType.UnsignedTinyInt |
是/否 | Boolean | DBTYPE_BOOL | System.Boolean | OleDbType.Boolean |
日期 / 时间 | DateTime | DBTYPE _ DATE | System.DateTime | OleDbType.date |
货币 | 十进制 | DBTYPE_NUMERIC | System.Decimal | OleDbType.numeric |
十进制数: | 十进制 | DBTYPE_NUMERIC | System.Decimal | OleDbType.numeric |
双精度数字: | 双精度数字 | DBTYPE_R8 | System.Double | OleDbType.Double |
自动数字(复制 ID) | GUID | DBTYPE_GUID | System.Guid | OleDbType.guid |
复制 (ID) 号: | GUID | DBTYPE_GUID | System.Guid | OleDbType.guid |
自动数字(长整型) | 整数 | DBTYPE_I4 | System.Int 32 | OleDbType.integer |
数量: (长整型) | 整数 | DBTYPE_I4 | System.Int 32 | OleDbType.integer |
OLE 对象 | LongVarBinary | DBTYPE_BYTES | 数组 System.Byte | OleDbType.LongVarBinary |
单精度数字: | 单精度数字 | DBTYPE_R4 | System.Single | OleDbType.single |
整型数: | SmallInt | DBTYPE_I2 | System.Int 16 | OleDbType.SmallInt |
二进制 | VarBinary * | DBTYPE_BYTES | 数组 System.Byte | OleDbType.binary |
超链接 | VarWChar | DBTYPE _ WSTR | System.String | OleDbType.VarWChar |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

1. Open settings in Windows 11. You can use Win+I shortcut or any other method. 2. Go to the Apps section and click Apps & Features. 3. Find the application you want to prevent from running in the background. Click the three-dot button and select Advanced Options. 4. Find the [Background Application Permissions] section and select the desired value. By default, Windows 11 sets power optimization mode. It allows Windows to manage how applications work in the background. For example, once you enable battery saver mode to preserve battery, the system will automatically close all apps. 5. Select [Never] to prevent the application from running in the background. Please note that if you notice that the program is not sending you notifications, failing to update data, etc., you can

DeepSeek cannot convert files directly to PDF. Depending on the file type, you can use different methods: Common documents (Word, Excel, PowerPoint): Use Microsoft Office, LibreOffice and other software to export as PDF. Image: Save as PDF using image viewer or image processing software. Web pages: Use the browser's "Print into PDF" function or the dedicated web page to PDF tool. Uncommon formats: Find the right converter and convert it to PDF. It is crucial to choose the right tools and develop a plan based on the actual situation.

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app

DAO (Data Access Object) in Java is used to separate application code and persistence layer, its advantages include: Separation: Independent from application logic, making it easier to modify it. Encapsulation: Hide database access details and simplify interaction with the database. Scalability: Easily expandable to support new databases or persistence technologies. With DAOs, applications can call methods to perform database operations such as create, read, update, and delete entities without directly dealing with database details.

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

In Java, a "field" is a data member in a class or interface that is used to store data or state. The properties of field include: type (can be any Java data type), access rights, static (belongs to a class rather than an instance), final (immutable) and transient (not serialized). Field is used to store state information of a class or interface, such as storing object data and maintaining object state.

Oracle stored procedure execution plans provide execution information, including access paths, estimated number of rows, join order, and costs. To view the execution plan, execute the EXPLAIN PLAN command and look for the "Execution Plan" section. The execution plan contains a header and body, showing in detail the ID, operation type, number of rows, cost, access path, filter conditions, involved tables and indexes, and the connection sequence if there is a connection.

The Java reflection mechanism allows programs to dynamically modify the behavior of classes without modifying the source code. By operating the Class object, you can create instances through newInstance(), modify private field values, call private methods, etc. Reflection should be used with caution, however, as it can cause unexpected behavior and security issues, and has a performance overhead.
