如何在 Access 2003 和 Access 2002 中创建 DSN 的连接到 SQLServer 对链接表
2018-10-12 14:10
1006 查看
方法 1: 使用 CreateTableDef 方法
CreateTableDef 方法可创建链接表。 若要使用此方法, 创建一个新模块, 然后以下 AttachDSNLessTable 函数添加到新模块。
若要调用 AttachDSNLessTable 函数, 请代码, 它类似于之一以下代码示例在 Autoexec 宏中或启动窗体 Form_Open 事件中:
若要调用 CreateDSNConnection 函数, 请代码, 它类似于之一以下代码示例在 Autoexec 宏中或启动窗体 Form_Open 事件中:
注意 此方法假定通过使用 " 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)
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 事件。 向 Access 数据库添加多个链接表时 注意 您必须调整编程逻辑。Private Sub Form_Open(Cancel As Integer) If AttachDSNLessTable("authors", "authors", "(local)", "pubs", "", "") Then '// All is okay. Else '// Not okay. End If End Sub |
方法 2: 使用 DAO.RegisterDatabase 方法
DAO.RegisterDatabase 方法可在 Autoexec 宏中或启动表单中创建 DSN 连接。 尽管此方法不删除对 DSN 连接, 要求它不帮助您通过代码中创建 DSN 连接解决问题。 若要使用此方法, 创建一个新模块, 然后以下 CreateDSNConnection 函数添加到新模块。注意 如果再次, 调用 RegisterDatabase 方法 DSN 更新。'//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
若要调用 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 If End Sub |
请 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)
您可能感兴趣的文章:
- 随机提取Access/SqlServer数据库中的10条记录的SQL语句
- ACCESS转SQLSERVER数据库的注意事项
- Access转SqlServer的注意事项
- asp.net 数据库备份还原(sqlserver+access)
- SQL 随机查询 包括(sqlserver,mysql,access等)
- Excel导入Sqlserver数据库脚本
- ASP将Excel数据导入到SQLServer的实现代码
- ADO.NET 连接数据库字符串小结(Oracle、SqlServer、Access、ODBC)
- 解析SQLServer获取Excel中所有Sheet的方法
- 将ACCESS数据库迁移到SQLSERVER数据库两种方法(图文详解)
- 将excel高效导入sqlserver的可行方法
- SQL SERVER 2008 64位系统无法导入ACCESS/EXCEL怎么办
相关文章推荐
- 如何在 Access 2003 和 Access 2002 中创建 DSN 的连接到 SQLServer 对链接表
- 请问在VC++2010中如何连接用Access2010创建好的accdb数据库?
- sqlserver如何创建链接服务器
- Sqlserver中如何创建链接服务器
- 创建Accress 数据库连接文件UDL/如何使用 ADO 的数据链接文件
- Sqlserver创建连接MySql的链接服务器
- sqlserver如何创建链接服务器
- ASP.NET如何连接Access或SQL Server数据库
- linux 链接ln的使用 创建和删除符号连接(软、硬链接)
- 在Access中创建表及如何指定字段类型
- sqlserver中创建链接服务器
- ACCESS 中如何用 WSH 创建桌面快捷方式
- 创建框架链接--frameset的连接方法
- 利用【数据链接属性】对话框,创建连接字符串
- 在sql server 中创建链接服务器连接mysql数据库
- 如何正确获取MySql/Access的ADO连接字符串
- sqlserver中创建链接服务器图解教程
- .net 连接DB2数据库 ,采用sqlserver 2000的链接服务解决方案总结
- visio 2003 如何反向连接db2生成er图
- QT ODBC方式连接MS SQLSERVER2000/2005、ACCESS示例 (字符串方式,不用建DSN)