濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 如何在 Access 2003 和 Access 2002 中創(chuàng)建 DSN 的連接到 SQLServer 對(duì)鏈接表

如何在 Access 2003 和 Access 2002 中創(chuàng)建 DSN 的連接到 SQLServer 對(duì)鏈接表

熱門標(biāo)簽:修改地圖標(biāo)注 萊西電子地圖標(biāo)注 金昌電話機(jī)器人價(jià)格 縣域地圖標(biāo)注打印店 怎么在地圖標(biāo)注自己 外呼系統(tǒng)API接口 武夷山旅游地圖標(biāo)注 鳳臺(tái)百度地圖標(biāo)注店 個(gè)人可以辦理400電話么
方法 1: 使用 CreateTableDef 方法
CreateTableDef 方法可創(chuàng)建鏈接表。 若要使用此方法, 創(chuàng)建一個(gè)新模塊, 然后以下 AttachDSNLessTable 函數(shù)添加到新模塊。
復(fù)制代碼 代碼如下:

'//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


若要調(diào)用 AttachDSNLessTable 函數(shù), 請(qǐng)代碼, 它類似于之一以下代碼示例在 Autoexec 宏中或啟動(dòng)窗體 Form_Open 事件中:
當(dāng)您使用 Autoexec, 調(diào)用 AttachDSNLessTable 函數(shù), 并然后傳遞參數(shù), 如以下所示從 RunCode 操作。
  AttachDSNLessTable ("authors", "authors", "(local)", "pubs", "", "")
當(dāng)您使用啟動(dòng)窗體, 將代碼, 它類似于以下以 Form_Open 事件。
Private Sub Form_Open(Cancel As Integer)
  If AttachDSNLessTable("authors", "authors", "(local)", "pubs", "", "") Then
    '// All is okay.
  Else
    '// Not okay.
  End If
End Sub
向 Access 數(shù)據(jù)庫(kù)添加多個(gè)鏈接表時(shí) 注意 您必須調(diào)整編程邏輯。
 

方法 2: 使用 DAO.RegisterDatabase 方法

DAO.RegisterDatabase 方法可在 Autoexec 宏中或啟動(dòng)表單中創(chuàng)建 DSN 連接。 盡管此方法不刪除對(duì) DSN 連接, 要求它不幫助您通過(guò)代碼中創(chuàng)建 DSN 連接解決問(wèn)題。 若要使用此方法, 創(chuàng)建一個(gè)新模塊, 然后以下 CreateDSNConnection 函數(shù)添加到新模塊。
'//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 password
Function 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 Function
CreateDSNConnection_Err:
  
  CreateDSNConnection = False
  MsgBox "CreateDSNConnection encountered an unexpected error: "  Err.Description
  
End Function
注意 如果再次, 調(diào)用 RegisterDatabase 方法 DSN 更新。

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

請(qǐng) CreateTableDef 方法, 有關(guān)訪問(wèn)下列 Microsoft Developer Network (MSDN) Web 站點(diǎn):
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)
有關(guān) RegisterDatabase 方法, 請(qǐng)?jiān)L問(wèn)以下 MSDNWeb 站點(diǎn):
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)
您可能感興趣的文章:
  • 隨機(jī)提取Access/SqlServer數(shù)據(jù)庫(kù)中的10條記錄的SQL語(yǔ)句
  • ACCESS轉(zhuǎn)SQLSERVER數(shù)據(jù)庫(kù)的注意事項(xiàng)
  • Access轉(zhuǎn)SqlServer的注意事項(xiàng)
  • asp.net 數(shù)據(jù)庫(kù)備份還原(sqlserver+access)
  • SQL 隨機(jī)查詢 包括(sqlserver,mysql,access等)
  • Excel導(dǎo)入Sqlserver數(shù)據(jù)庫(kù)腳本
  • ASP將Excel數(shù)據(jù)導(dǎo)入到SQLServer的實(shí)現(xiàn)代碼
  • ADO.NET 連接數(shù)據(jù)庫(kù)字符串小結(jié)(Oracle、SqlServer、Access、ODBC)
  • 解析SQLServer獲取Excel中所有Sheet的方法
  • 將ACCESS數(shù)據(jù)庫(kù)遷移到SQLSERVER數(shù)據(jù)庫(kù)兩種方法(圖文詳解)
  • 將excel高效導(dǎo)入sqlserver的可行方法
  • SQL SERVER 2008 64位系統(tǒng)無(wú)法導(dǎo)入ACCESS/EXCEL怎么辦

標(biāo)簽:楚雄 赤峰 南京 上海 通遼 涼山 清遠(yuǎn) 邢臺(tái)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《如何在 Access 2003 和 Access 2002 中創(chuàng)建 DSN 的連接到 SQLServer 對(duì)鏈接表》,本文關(guān)鍵詞  如,何在,Access,2003,和,2002,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《如何在 Access 2003 和 Access 2002 中創(chuàng)建 DSN 的連接到 SQLServer 對(duì)鏈接表》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于如何在 Access 2003 和 Access 2002 中創(chuàng)建 DSN 的連接到 SQLServer 對(duì)鏈接表的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    南召县| 峨眉山市| 绿春县| 象州县| 和平区| 勃利县| 额敏县| 绥江县| 平阴县| 嘉黎县| 屏东县| 顺平县| 河北省| 常宁市| 新民市| 金阳县| 南和县| 安义县| 祁东县| 台北市| 桐柏县| 长岭县| 山西省| 昌江| 东光县| 广东省| 临洮县| 景泰县| 叶城县| 翁牛特旗| 宝坻区| 彭泽县| 东乡县| 奉化市| 台北县| 东丽区| 资源县| 合作市| 南陵县| 广南县| 巢湖市|