What is the difference between using currentproject.connection vs connection strings for ADO object?

别说谁变了你拦得住时间么 提交于 2019-12-31 06:22:03

问题


We can set up the connection string for ADO connection using either currentproject.connection or using a DSN (DSN=MyDSN;UID=MyID;PWD=MyPwd) or DSN-Less connection string(

DRIVER=\{SQL Server\};SERVER=
MyServer;DATABASE=pubs;
UID=MyID;PWD=MyPwd)

My question is specifically in the case of linked tables connected to SQL Server.

I have a DSN-less linked tables connected to SQL Server backend. I used this code provided by microsoft to create the DSN-less connection. If I do

debug.print currentproject.connection I get something like

Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=C:\Users\DAVE\Desktop\DATA.accdb;Mode=Share Deny None;Extended Properties="";Jet OLEDB:System database=C:\Users\DAVE\AppData\Roaming\Microsoft\Access\System3.mdw;Jet OLEDB:Registry Path=Software\Microsoft\Office\16.0\Access\Access Connectivity Engine;Jet OLEDB:Database Password="";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=True;Jet OLEDB:Bypass UserInfo Validation=False;Jet OLEDB:Limited DB Caching=False;Jet OLEDB:Bypass ChoiceField Validation=False

I guess, the above string is converted into a dsn-less connection string looking like this :

DRIVER=\{SQL Server\};SERVER=
MyServer;DATABASE=pubs;
UID=MyID;PWD=MyPwd)

Accounting for this conversion speed, is it faster to use a connection string rather than currentproject.connection?


回答1:


CurrentProject.Connection is a pre-initialized and ever present connection to the current Access database.

Connections using ODBC connection strings, OLEDB connection strings or ODBC DSNs, however, can connect directly to external data sources, reducing overhead and using the SQL dialect, options and quirks of the external system.

CurrentProject.Connection can make use of linked tables, but if it does, it actually uses a DAO connection connecting Access to the SQL server (linked tables always use DAO since Access uses DAO internally), and then an ADO connection to the Access database to retrieve the data from Access, causing additional overhead.

This means that connections using either the SQL server ODBC driver or OLEDB provider can be substantially faster for specific cases than those using CurrentProject.Connection when connecting to external sources. Also, direct connections using the ODBC driver or OLEDB provider can expose additional functionality, such as returning information messages from SQL server.


To elaborate further on what happens:

If you use CurrentProject.Connection to open a linked table, the following happens:

  1. You use an already opened connection to the Access database
  2. You send an SQL command to the Access database using it's dialect of SQL (JET/ACE SQL using SQL server compatible syntax mode)
  3. Access identifies the table that it should query, identifies that it is linked, and translates the SQL statement to the appropriate language
  4. Access either opens up a new (internal/DAO) connection to SQL server, or uses an existing open connection if it exists
  5. Access pulls in data from SQL server to the Access database using the connection it decided on and the translated statement
  6. Access then sends the data to the ADODB connection.

If you use a direct ADODB connection, however, the process is quite a lot more simple:

  1. You open a new ADODB connection to SQL server
  2. You send an SQL command to SQL server
  3. SQL server sends the data back to you using the ADODB connection

The Access database engine is 100% unaware that you're querying anything here, since it's not involved in any of the steps.

Further notes:

  • If you use CurrentProject.Connection and a server-side cursor, the cursor is managed by the Access Database Engine, not by SQL server, even though you're querying SQL server.

  • If you use CurrentProject.Connection you have to use Access (JET/ACE) SQL using SQL server compatible syntax. This means that in LIKE statements, you need to use % as wildcard, and you have a different feature set than using the normal syntax, but you still have to use octothorpes when delimiting calls and the SQL statement is still processed by Access, not SQL server.

  • While as a general statement, a direct connection to SQL server is faster than connecting through Access using CurrentProject.Connection, exceptions may apply (for example, because Access already has an open connection and you're only executing a small statement so the relative overhead of establishing a new connection is large)



来源:https://stackoverflow.com/questions/54136299/what-is-the-difference-between-using-currentproject-connection-vs-connection-str

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!