关于从MS Access使用SQL Server GUID的建议 [英] Recommendations on using SQL Server GUID from MS Access

查看:146
本文介绍了关于从MS Access使用SQL Server GUID的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将现有的MS Access后端升级到SQL Server 2008,因为我们想使用 SQL Server合并复制,我必须将所有当前主键(当前标准的自动增量整数)更改为GUID。



所以这里有问题:




  • 有关将主键从整数更改为GUID的任何建议?

  • 任何关于使用和操作GUID的建议从Access客户端的代码?

  • 我应该使用哪个SQL Server GUID类型?


解决方案

克里斯是对的说(1)你不需要GUIDS合并复制和(2)只有一个GUID类型,但你必须知道:


  1. 可以按照不同的规则生成GUIDS。您可以查看此此处

  2. 设置复制时,如果
    不存在,SQL将系统地将$ GU $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
    $ c> rowguid 。当然,如果您在每个表中都有这样的GUID / newSequentialId,SQL将会使用它。 BUt我不建议你使用PK GUID复制复制GUID:你可以将GUID类型的所有主键声明为newSequentialIds,但是(a)然后会放弃在客户端生成GUID值的可能性 - 见下文 - 和(b)你的PK将是'可预测的',这个想法让我感到不舒服...

  3. 保持自动增量整数和通过复制管理他们的范围意味着很多的开销(您必须为每个表/每个出版物分配范围)和复制来自不同来源的潜在的冲突来源。

  4. 此外,一些SQL错误,如这一个特定于范围分配的仍然没有正确解决:应用累积包5没有解决我们的问题,我们必须找到另一种方式重新启动我们的复制过程。

  5. 无论如何,我深信从整数切换到GUID因为主键是强制性的。有很多原因,其中一个与这些范围管理相关联,作为头痛和隔夜麻烦的潜在来源。关于从整数到GUIDS的更改,我建议您编写一个分步模块,该模块将:



$ b <



花点时间写这段代码。在使之正常工作之前,您将多次使用它。您应该利用DAO对象,tabledefs,索引等。请记住,必须能够回到起点,所以不要忘记初始备份过程。



什么关于操作VBA中的GUID?有一些事情要知道:








 ? myForm.myControl 


? myForm.recordset.fields(myFieldName)
{000581EB-9CBF-418C-A2D9-5A7141A686CC}




  • 您可能必须在使用诸如recordset.findfirst之类的表达式导航记录集时将您的guids转换为字符串:






  myFirstRecordset.FindFirststringFromGUID(myGuidId)=& StringFromGUID(mySecondRecordset.Fields(myGuidId)。值)


I'm upsizing an existing MS Access backend to SQL Server 2008 and, because we want to use SQL Server Merge replication, I'll have to change all current primary keys (currently standard autoincrement integers) to GUID.

So here are the questions:

  • Any recommendation on doing the change of primary keys from integer to GUID?
  • Any recommendation on using and manipulating the GUID from code from within Access clients?
  • Which SQL Server GUID type should I use?

解决方案

Chris is right when saying that (1) you do not need GUIDS for merge replication and (2) there is only one GUID type, but you have to know that:

  1. GUIDS can be generated following different rules. You can check this here
  2. When setting a replication, SQL will systematically add a GUID (generated as a newsequentialid) to each table if it does not already exist, and will call it rowguid. Of course, if you already have such a GUID/newSequentialId in each table, SQL will make use of it. BUt I do not advise you to 'mix' replication GUIDs with PK GUIDs: you could declare all your primary keys of the GUID type as 'newSequentialIds', but (a) you would then loose the possibility to generate GUID values on the client's side - see infra - and (b) your PKs will then be 'predictable', and this idea makes me feel uncomfortable...
  3. keeping autoincrement integers and managing their range through replication means a lot of overhead (you have to allocate ranges for each table/each publication) and a potential source of conflicts when replicating from different sources.
  4. Moreover, some SQL bugs like this one, which is specific to range allocation, are still not properly solved: applying cumulative pack 5 did not solve our problem and we had to find another way to restart our replication processes.
  5. Anyway, I am deeply convinced that switching from integers to GUIDs as primary keys is mandatory. There are many reasons for that, and one of them is linked to these range management as a potential source for headacke and overnight troubleshouting sessions.

Concerning the change from integers to GUIDS, I advise you to write a step-by-step module that will:

  • Backup up all existing tables before modifying them
  • Add a GUID field to each table
  • Add corresponding FK fields where requested
  • Update FK fields through views built with the existing relations (built on integer fields)
  • Break relations
  • Change PKs from integer fields to GUID fields
  • Recreate relations

Take your time to write this code. You will use it many times before having it properly working. You should make profit of the DAO object, tabledefs, indexes, and so on. Keep in mind that you must allways be able to go back to the starting point, so don't forget the initial backup process.

What about manipulating GUIDs from VBA? There are a few things to know about that:

  • GUIDs are of the Variant type
  • It is possible and easy to generate your GUID as primary key on the client's side of the app, as I proposed it once here.
  • When you try to get a GUID value from a control in a form (usually as the linked field in a combobox), you'll get '?????'but no value. You have to refer to the field value in the recordset to get the correct data. You can open such a form in your app, go to the 'immediate' window, and try this:

? myForm.myControl
?????

? myForm.recordset.fields("myFieldName")
{000581EB-9CBF-418C-A2D9-5A7141A686CC}

  • You might have to convert your guids to strings when navigating through recordsets with expressions such as recordset.findfirst:

myFirstRecordset.FindFirst "stringFromGUID(myGuidId) = " & StringFromGUID(mySecondRecordset.Fields("myGuidId").Value)

这篇关于关于从MS Access使用SQL Server GUID的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆