简体   繁体   English

将多用户访问前端连接到SQL Server后端

[英]Connecting a Multi User Access Front End to SQL Server Back End

Thank you for your help as always! 一如既往地感谢您的帮助!

I have built an Access database that we intend to distribute to ~100 end users. 我已经构建了一个Access数据库,我们打算将其分发给大约100个最终用户。 I am currently running a pilot program for 5 users. 我目前正在为5个用户运行试点计划。 I have migrated the tables to SQL 2014 Server but currently the back end of the pilot program is in a shared network folder (location unknown to them). 我已将表迁移到SQL 2014 Server,但目前试验程序的后端位于共享网络文件夹中(位置未知)。 I distributed the Access front end (accde file type) by saving the file on that shared Network and instructing the end users to save a copy to their desktop. 我通过在该共享网络上保存文件并指示最终用户将副本保存到其桌面来分发Access前端(accde文件类型)。 The pilot is working fine, if not a little slow. 飞行员工作正常,如果不是有点慢。

The issue that I am having is this: 我遇到的问题是:

I am able to set up an ODBC Connection on my machine and am able to connect to the SQL Server backend through that connection. 我能够在我的机器上设置ODBC连接,并能够通过该连接连接到SQL Server后端。 However, I am not sure how to get my end users Access to the tables on the server. 但是,我不知道如何让我的最终用户访问服务器上的表。 Do I need to create a user name for each user on the server and give them read and write access? 我是否需要为服务器上的每个用户创建一个用户名并为其提供读写访问权限? Do I also need to create an ODBC connection on each machine that we plan to install the Front End on? 我是否还需要在计划安装前端的每台计算机上创建ODBC连接? I would prefer to not have to create an ODBC connection on each machine, is it possible to work around this? 我宁愿不必在每台机器上创建ODBC连接,是否可以解决这个问题? Thank you! 谢谢!

Access Version: 2013 SQL: 2014 Working on SSMS 2014 访问版本:2013 SQL:2014致力于SSMS 2014

-Charlie -Charlie

A few things: 一些东西:

When you create a linked table, simply use the ribbon import and link – and then ODBC database. 创建链接表时,只需使用功能区导入和链接 - 然后使用ODBC数据库。 Just choose a file DSN. 只需选择文件DSN即可。 The reason for this is that access by DEFAULT will use a DSN-less connection. 原因是DEFAULT的访问将使用无DSN连接。 In simple terms, this means when you link the tables, then you can distribute your applcatation to each workstation and there no need to setup a SYSTEM/Machine DSN. 简单来说,这意味着当您链接表格时,您可以将您的applcatation分发到每个工作站,而无需设置SYSTEM / Machine DSN。

So just keep in mind that use the default file DSN – once Access creates the link to SQL server, then such links are DSN-less, and you don't need any setup on each workstation. 所以请记住,使用默认文件DSN - 一旦Access创建到SQL Server的链接,那么这些链接是无DSN的,并且您不需要在每个工作站上进行任何设置。

As for creating users on SQL server? 至于在SQL服务器上创建用户? Well, you likely don't need to unless you want some kind of special security for each user. 好吧,除非你想为每个用户提供某种特殊的安全性,否则你可能不需要。 If you using SQL logon, then MAKE sure that during the above linking process you “check” the save password option. 如果您使用SQL登录,请确保在上述链接过程中“检查”保存密码选项。 Once again, since by default the linked tables are DSN-less, then every user will in fact be using the one and same SQL User/password and thus this will be transparent to each user (they will not have to log on). 再次,因为默认情况下链接表是DSN-less,然后每个用户实际上将使用同一个SQL用户/密码,因此这对每个用户都是透明的(他们不必登录)。

If you using windows authentication for SQL logons, then security is setup with the windows system and not SQL server. 如果您使用Windows身份验证进行SQL登录,则使用Windows系统而非SQL服务器设置安全性。 In this case then each users windows logon will be used to control (permit) use of the SQL server. 在这种情况下,每个用户的Windows登录将用于控制(允许)使用SQL服务器。 If you not using a domain controller, then you be using SQL logons, and likely just the one logon that you are using will suffice. 如果您不使用域控制器,那么您将使用SQL登录,并且可能只是您正在使用的一个登录就足够了。 Often even in a corporate environment because I don't want to call up the IT admin folks for each logon and permissions to SQL server, then I still OFTEN choose SQL logons. 通常即使在企业环境中,因为我不想为每个登录和SQL服务器的权限调用IT管理员,那么我仍然选择SQL登录。 Thus “once” the IT admin folks give me enough rights to the SQL server, then I am free to create my own logons, or just use the “one same” logon for everyone and thus don't have to waste time bothering the IT folks. 因此,“曾经”IT管理员给我足够的SQL服务器权限,然后我可以自由创建我自己的登录,或者只是为每个人使用“同一个”登录,因此不必浪费时间打扰IT乡亲。

A few additional final points: Ignore suggesting to use all kinds of ADO and VBA code and connection strings etc. – they are not required. 还有一些额外的要点:忽略建议使用各种ADO和VBA代码和连接字符串等 - 它们不是必需的。 In fact in most cases you want to AVOID ADO code in your application. 事实上,在大多数情况下,您希望在应用程序中避免使用ADO代码。 And oleDB is being depreciated for SQL server (which ADO tends to rely on). 而且,对于SQL服务器(ADO倾向于依赖),oleDB正在被折旧。

You STILL as a matter of good deployment want to place the front end program you have on each workstation. 您仍然需要将每个工作站上的前端程序放在一起。 Just like you install word on each workstation, or your accounting packages, now that YOU ARE developing software, then you install your software on each workstation like the IT industry done for the last 30 years. 就像你在每个工作站或会计软件包上安装文字一样,既然你正在开发软件,那么你就像过去30年来IT行业那样在每个工作站上安装你的软件。 You can certainly share data on a shared folder, but you install the actual application (word, Excel, or in this case YOUR applcatation on EACH workstation. And you should compile the accDB to an accDE before any deployment. 您当然可以在共享文件夹上共享数据,但是您可以安装实际的应用程序(word,Excel,或者在这种情况下,您可以在EACH工作站上安装。)在任何部署之前,您应该将accDB编译为accDE。

So you don't really need any special code on start up to “connect” or “link” to SQL server if your deployment to such users is on your same network. 因此,如果您对这些用户的部署位于同一网络上,那么启动“连接”或“链接”到SQL服务器时,您并不需要任何特殊代码。 If you are developer or consultant “offsite”, then you likely need to add some code on start up to re-link to THEIR sql server that no doubt would be different than the one your developing with offsite. 如果您是“非现场”的开发人员或顾问,那么您可能需要在启动时添加一些代码以重新链接到他们的sql服务器,这无疑会与您在异地开发的服务器不同。 So some ability to re-link to a “different” SQL server then the one your are developing with would be required if you not able to develop on site, or that the SQL server you working with is a “copy” or “test” version of the Actual production SQL server being used. 因此,如果您无法在现场开发,或者您使用的SQL服务器是“复制”或“测试”,则需要一些能够重新链接到“不同”SQL服务器然后正在开发的SQL服务器的能力。正在使用的实际生产SQL服务器的版本。

You do not need an ODBC connection on every machine. 每台计算机上都不需要ODBC连接。 You can create the connection by using ODBC connection strings via DoCmd.TransferDatabase . 您可以通过DoCmd.TransferDatabase使用ODBC连接字符串来创建连接。 This is persistent, so you can just execute the connection/updates on your development copy of the front end and the connection will exist and persist in any end user copies of that that you make. 这是持久性的,因此您可以在前端的开发副本上执行连接/更新,并且连接将存在并保留在您所创建的任何最终用户副本中。

You do have to deal with the authentication issue. 您必须处理身份验证问题。 Depending on your security situation you can create a single "database user" and include the username and password to the above connection. 根据您的安全情况,您可以创建一个“数据库用户”,并在上述连接中包含用户名和密码。 If you are in a AD environment you can use that for authentication. 如果您在AD环境中,则可以使用它进行身份验证。 Or you create individual SQL accounts for each user or user group. 或者,您为每个用户或用户组创建单独的SQL帐户。 No matter what you do the users will be able to see and edit the tables if Access is to have read/write permission to them. 无论您做什么,如果Access具有对它们的读/写权限,用户将能够查看和编辑表。 You can secure individual tables by using views to link them to Access instead of the tables themselves. 您可以使用视图将它们链接到Access而不是表本身来保护单个表。

I would advise against having users make a local copy of the front end. 我建议不要让用户制作前端的本地副本。 That makes it a nightmare to make sure your users are using the latest version of your front end. 这使得确保您的用户使用最新版本的前端成为一场噩梦。 Encourage them to make a shortcut instead. 鼓励他们改为做捷径。 Unless you have per-user local temp tables or code that dynamically edits forms/reports there is not much reason not to have everybody using the same front end file. 除非你有每个用户的本地临时表或动态编辑表单/报告的代码,否则没有太多理由不让每个人都使用相同的前端文件。 Even in that case create a deployment batch file that creates/overwrites the front end copy for each user. 即使在这种情况下,也要创建一个部署批处理文件,为每个用户创建/覆盖前端副本。

Sample code that links to SQL server database: 链接到SQL Server数据库的示例代码:

Public Sub LoadDatabase()

Dim MyDB As DAO.Database
Set MyDB = CurrentDb
Dim d As DAO.Recordset
Set d = MyDB.OpenRecordset("SELECT TableName FROM tblLinkTables")
d.MoveFirst
Do While Not d.EOF
    DoCmd.DeleteObject acTable, d!TableName
    DoCmd.TransferDatabase acLink, "ODBC Database", "ODBC;Driver={SQL Server};Server=MYSQLSERVERNAME;Database=MyDatabaseName;Uid=DatabaseUser;Pwd=DatabaseUserPassword", acTable, d!TableName, d!TableName, False, True
    d.MoveNext
  Loop
d.Close
Set d = Nothing

End Sub

where the list of tables to link exists on a local table called tblLinkTables with a single field TableName for each table to be linked. 要链接的表的列表存在于名为tblLinkTables的本地表上,其中每个表都要链接一个字段TableName。 This is a very simplified version. 这是一个非常简化的版本。 The one I actually use has the capability to have different local table names than the SQL tables and also link views and create primary keys (optionally) for them. 我实际使用的那个能够具有与SQL表不同的本地表名,还能链接视图并为它们创建主键(可选)。

The following is one example of how you can obtain a connection to SQL Server. 以下是如何获取与SQL Server的连接的一个示例。 It supports using either a Trusted Connection (where the windows user is authorized access, or you can specify a sqlserver defined userid and password. 它支持使用可信连接(Windows用户被授权访问,或者您可以指定sqlserver定义的用户ID和密码)。

' The following are some of the parameters the connection will use.
Global gv_DBS_SQLServer                 As ADODB.Connection
Global gvstr_SQLServer_Name             As String
Global gv_SQLServer_DSN                 As String
Global gvstr_SQLServer_Database         As String

'Call the GetConnection Function
' Pass Name of Server; Database Name; Connection Variable; adUseServer; True if using Password; False if not using a Trusted Connection;
' Sample
    If GetConnection(gvstr_SQLServer_Name, gvstr_SQLServer_Database, _
            gv_DBS_SQLServer, adUseServer, True, False) = False Then
        MsgBox "Unable to connect to SQL Server", vbOKOnly, "No Connection"
    End If


Public Function GetConnection(ByVal strDSN As String, _
        ByVal strDatabase As String, _
        ByRef cnLocal As ADODB.Connection, _
        ByVal CursorLoc As CursorLocationEnum, _
        ByVal UsePassword As Boolean, _
        ByVal blnTrusted As Boolean) As Boolean

Dim strConnectString    As String
Dim strDisplay          As String

    On Error GoTo ERROR_HANDLER
    GetConnection = False
Retry_Connection:
    If cnLocal Is Nothing Then Set cnLocal = New ADODB.Connection
    If cnLocal.State = adStateOpen Then
        Debug.Print "Connection already open -- -will not reopen!!"
        GetConnection = True
        GoTo Proc_Exit
    End If
    With cnLocal
        Debug.Print "Use TRUSTED CONNECTION (ABOVE)"
        If blnTrusted = True Then
            strConnectString = "Driver={SQL Server};" & _
                               "Server=" & strDSN & ";" & _
                               "Database=" & strDatabase & ";" & _
                               "Trusted_Connection=yes"
        Else
            strConnectString = "Driver={SQL Server};" & _
                               "Server=" & strDSN & ";" & _
                               "Database=" & strDatabase & ";" & _
                               "User Id=UUUUUUU;Password=" & DecryptString("PPPPPPPP") & ""

            strDisplay = "Driver={SQL Server};" & _
                         "Server=" & strDSN & ";" & _
                         "Database=" & strDatabase & ";" & _
                         "User Id=UUUUUU;Password=PPPPPPP"

        End If

         Debug.Print "Will use Conn String: " & strDisplay
        .ConnectionString = strConnectString
        .CursorLocation = CursorLoc
        .Open
    End With
    GetConnection = True
Proc_Exit:
    Exit Function
ERROR_HANDLER:
    Debug.Print Err.Number & vbCrLf & Err.Description
    Err.Source = "Module_Connect: " 
    DocAndShowError
    Resume Proc_Exit
    Resume Next
    Resume
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM