国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > ASP > 正文

如何在 Access 2003 和 Access 2002 中創建 DSN 的連接到 SQLServer 對鏈接表

2020-07-27 13:25:57
字體:
來源:轉載
供稿:網友
方法 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 事件。
Private Sub Form_Open(Cancel As Integer)  If AttachDSNLessTable("authors", "authors", "(local)", "pubs", "", "") Then    '// All is okay.  Else    '// Not okay.  End IfEnd Sub
向 Access 數據庫添加多個鏈接表時 注意 您必須調整編程邏輯。
 

方法 2: 使用 DAO.RegisterDatabase 方法

DAO.RegisterDatabase 方法可在 Autoexec 宏中或啟動表單中創建 DSN 連接。 盡管此方法不刪除對 DSN 連接, 要求它不幫助您通過代碼中創建 DSN 連接解決問題。 若要使用此方法, 創建一個新模塊, 然后以下 CreateDSNConnection 函數添加到新模塊。
'//Name   :  CreateDSNConnection'//Purpose :  Create a DSN to link tables to SQL Server'//Parameters'//   stServer: Name of 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 passwordFunction CreateDSNConnection(stServer As String, stDatabase As String, Optional stUsername As String, Optional stPassword As String) As Boolean  On Error GoTo CreateDSNConnection_Err  Dim stConnect As String    If Len(stUsername) = 0 Then    '//Use trusted authentication if stUsername is not supplied.    stConnect = "Description=myDSN" & vbCr & "SERVER=" & stServer & vbCr & "DATABASE=" & stDatabase & vbCr & "Trusted_Connection=Yes"  Else    stConnect = "Description=myDSN" & vbCr & "SERVER=" & stServer & vbCr & "DATABASE=" & stDatabase & vbCr   End If    DBEngine.RegisterDatabase "myDSN", "SQL Server", True, stConnect      '// Add error checking.  CreateDSNConnection = True  Exit FunctionCreateDSNConnection_Err:    CreateDSNConnection = False  MsgBox "CreateDSNConnection encountered an unexpected error: " & Err.Description  End Function
注意 如果再次, 調用 RegisterDatabase 方法 DSN 更新。

若要調用 CreateDSNConnection 函數, 請代碼, 它類似于之一以下代碼示例在 Autoexec 宏中或啟動窗體 Form_Open 事件中:
當您使用 Autoexec, 調用 CreateDSNConnection 函數, 并然后傳遞參數, 如以下所示從 RunCode 操作。
  CreateDSNConnection ("(local)", "pubs", "", "")
當您使用啟動窗體, 將代碼, 它類似于以下以 Form_Open 事件。
Private Sub Form_Open(Cancel As Integer)  If CreateDSNConnection("(local)", "pubs", "", "") Then    '// All is okay.  Else    '// Not okay.  End IfEnd Sub
注意 此方法假定通過使用 " myDSN " 作為 DSN 名稱, 您已經創建鏈接 SQLServer 表 Access 數據庫中。

CreateTableDef 方法, 有關訪問下列 Microsoft Developer Network (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)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桑植县| 孙吴县| 道真| 上思县| 九龙城区| 铜鼓县| 湖北省| 临安市| 共和县| 鲁山县| 平阳县| 沂南县| 康定县| 灵宝市| 玛纳斯县| 鄂托克旗| 遂川县| 治多县| 霍州市| 图木舒克市| 金寨县| 平湖市| 晋中市| 龙泉市| 逊克县| 婺源县| 会同县| 汉源县| 兴国县| 乌恰县| 禹城市| 武穴市| 芒康县| 福清市| 岳阳县| 陇西县| 太保市| 禹城市| 神木县| 长顺县| 南和县|