zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

如何在Access2003和Access2002中创建DSN的连接到SQLServer对链接表

SQLServer连接 如何 创建 dsn 链接表
2023-06-13 09:13:45 时间
方法 1: 使用 CreateTableDef 方法
CreateTableDef 方法可创建链接表。 若要使用此方法, 创建一个新模块, 然后以下 AttachDSNLessTable 函数添加到新模块。
复制代码代码如下:

"//Name     :   AttachDSNLessTable
"//Purpose  :   Create a linked table to SQL Server without using a DSN
"//Parameters
"//     stLocalTableName: Name of the table that you are creating in the current database
"//     stRemoteTableName: Name of the table that you are linking to on the SQL Server database
"//     stServer: Name of the SQL Server that you are linking to
"//     stDatabase: Name of the SQL Server database that you are linking to
"//     stUsername: Name of the SQL Server user who can connect to SQL Server, leave blank to use a Trusted Connection
"//     stPassword: SQL Server user password
Function AttachDSNLessTable(stLocalTableName As String, stRemoteTableName As String, stServer As String, stDatabase As String, Optional stUsername As String, Optional stPassword As String)
    On Error GoTo AttachDSNLessTable_Err
    Dim td As TableDef
    Dim stConnect As String

    For Each td In CurrentDb.TableDefs
        If td.Name = stLocalTableName Then
            CurrentDb.TableDefs.Delete stLocalTableName
        End If
    Next

    If Len(stUsername) = 0 Then
        "//Use trusted authentication if stUsername is not supplied.
        stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";Trusted_Connection=Yes"
    Else
        "//WARNING: This will save the username and the password with the linked table information.
        stConnect = "ODBC;DRIVER=SQL Server;SERVER=" & stServer & ";DATABASE=" & stDatabase & ";UID=" & stUsername & ";PWD=" & stPassword
    End If
    Set td = CurrentDb.CreateTableDef(stLocalTableName, dbAttachSavePWD, stRemoteTableName, stConnect)
    CurrentDb.TableDefs.Append td
    AttachDSNLessTable = True
    Exit Function

AttachDSNLessTable_Err:

    AttachDSNLessTable = False
    MsgBox "AttachDSNLessTable encountered an unexpected error: " & Err.Description

End Function


若要调用AttachDSNLessTable函数,请代码,它类似于之一以下代码示例在Autoexec宏中或启动窗体Form_Open事件中: 当您使用Autoexec,调用AttachDSNLessTable函数,并然后传递参数,如以下所示从RunCode操作。AttachDSNLessTable("authors","authors","(local)","pubs","","") 当您使用启动窗体,将代码,它类似于以下以Form_Open事件。PrivateSubForm_Open(CancelAsInteger) IfAttachDSNLessTable("authors","authors","(local)","pubs","","")Then "//Allisokay. Else "//Notokay. EndIf EndSub向Access数据库添加多个链接表时注意您必须调整编程逻辑。  
方法2:使用DAO.RegisterDatabase方法 DAO.RegisterDatabase方法可在Autoexec宏中或启动表单中创建DSN连接。尽管此方法不删除对DSN连接,要求它不帮助您通过代码中创建DSN连接解决问题。若要使用此方法,创建一个新模块,然后以下CreateDSNConnection函数添加到新模块。"//Name:CreateDSNConnection "//Purpose:CreateaDSNtolinktablestoSQLServer "//Parameters "//stServer:NameofSQLServerthatyouarelinkingto "//stDatabase:NameoftheSQLServerdatabasethatyouarelinkingto "//stUsername:NameoftheSQLServeruserwhocanconnecttoSQLServer,leaveblanktouseaTrustedConnection "//stPassword:SQLServeruserpassword FunctionCreateDSNConnection(stServerAsString,stDatabaseAsString,OptionalstUsernameAsString,OptionalstPasswordAsString)AsBoolean OnErrorGoToCreateDSNConnection_Err DimstConnectAsString IfLen(stUsername)=0Then "//UsetrustedauthenticationifstUsernameisnotsupplied. stConnect="Description=myDSN"&vbCr&"SERVER="&stServer&vbCr&"DATABASE="&stDatabase&vbCr&"Trusted_Connection=Yes" Else stConnect="Description=myDSN"&vbCr&"SERVER="&stServer&vbCr&"DATABASE="&stDatabase&vbCr EndIf DBEngine.RegisterDatabase"myDSN","SQLServer",True,stConnect "//Adderrorchecking. CreateDSNConnection=True ExitFunction CreateDSNConnection_Err: CreateDSNConnection=False MsgBox"CreateDSNConnectionencounteredanunexpectederror:"&Err.Description EndFunction 注意如果再次,调用RegisterDatabase方法DSN更新。

若要调用CreateDSNConnection函数,请代码,它类似于之一以下代码示例在Autoexec宏中或启动窗体Form_Open事件中: 当您使用Autoexec,调用CreateDSNConnection函数,并然后传递参数,如以下所示从RunCode操作。CreateDSNConnection("(local)","pubs","","") 当您使用启动窗体,将代码,它类似于以下以Form_Open事件。PrivateSubForm_Open(CancelAsInteger) IfCreateDSNConnection("(local)","pubs","","")Then "//Allisokay. Else "//Notokay. EndIf EndSub注意此方法假定通过使用"myDSN"作为DSN名称,您已经创建链接SQLServer表Access数据库中。

CreateTableDef方法,有关访问下列MicrosoftDeveloperNetwork(MSDN)Web站点: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/output/F1/D2/S5A289.asp(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/output/F1/D2/S5A289.asp)
有关RegisterDatabase方法,请访问以下MSDNWeb站点: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/output/F1/D2/S5A2EA.asp(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/output/F1/D2/S5A2EA.asp)