简体   繁体   English

使用ADO / DAO连接从SQL Server下载数据

[英]Using ADO/DAO Connection to Download data from SQL Server

I am trying to figure out how to download using an ADO/DAO connection in Access VBA to get the contents of a table from SQL server. 我试图弄清楚如何使用Access VBA中的ADO / DAO连接下载以从SQL Server获取表的内容。 I am trying to avoid using a linked table because the DB requires a password and I keep running into issues with getting it to not ask for the login info. 我试图避免使用链接表,因为数据库需要密码,而且我一直遇到问题,使其不询问登录信息。 Are there any ideas or references for me to start with on this matter? 在这件事上,我有什么想法或参考可以开始吗?

It appears either way you'll need to provide SQL credentials. 您将需要提供SQL凭据的两种方式出现。

There's more involved without linking a table, basically you'd want a recordset for the source and the "target" table to iterate over. 没有链接表,还有更多的事情要做,基本上,您希望对源和“目标”表的记录集进行迭代。

targetrs = CurrentDb.OpenRecordset("Target", dbOpenTable)

Dim Con As New ADODB.Connection
Dim sqlStr As String 
    Con.Open _
        "Provider = sqloledb;" & _
        "Data Source=SqlServer;" & _
        "Initial Catalog=MyDB;" & _
        "User ID=sa;" & _
        "Password=p@ssW0rd;"

Dim rsSource As New ADODB.Recordset 
    rsSource.Open "select * from SOURCE", Con

    do until rsSource.eof
        targetrs.addnew
        for each field in rsSource
            targetrs.fields(field.Name) = rsSource.fields(field.Name)
        next
        targetrs.update
        rssource.movenext
    loop

Since you still have to have the credentials, you could dynamically link the table instead: 由于仍然需要凭据,因此可以动态链接表:

docmd.TransferDatabase acLink,"ODBC Database",
"ODBC;Driver={SQL Server};Server=MySQLServer;Database=MYSQLDB;
Uid=USER;Pwd=PASSWORD",acTable,"SQLtable","MyAccessTable"

Use of a linked table does not require you store or have the user password in that linked table. 使用链接表不需要您在该链接表中存储或拥有用户密码。

If you execute a SINGLE logon at application startup then all linked tables will work. 如果您在应用程序启动时执行单次登录,则所有链接表都将起作用。

Linked tables work WITHOUT a prompt for user or password. 链接的表无需提示输入用户名或密码即可工作。

Linked tables work WITHOUT you having to store the user ID or password in the link. 链接表无需您在链接中存储用户ID或密码即可使用。

Access will cache the user name + password if you logon as per the instructions here: 如果您按照此处的说明登录,Access将缓存用户名和密码:

http://blogs.office.com/b/microsoft-access/archive/2011/04/08/power-tip-improve-the-security-of-database-connections.aspx http://blogs.office.com/b/microsoft-access/archive/2011/04/08/power-tip-improve-the-security-of-database-connections.aspx

So to download a table to a local, then you ONLY need this code: 因此,要将表下载到本地,则只需要以下代码:

For a new local table (create table query): 对于新的本地表(创建表查询):

CurrentDb.Execute "SELECT * INTO LocalTableCreate FROM ServerTable"

Append to existing table: 追加到现有表:

CurrentDb.Execute "INSERT INTO LocalTable SELECT * FROM ServerTable"

And if some really strange reason and desire exists create and promote world poverty and do things the hard way like a turtle with time to waste and not use a linked table? 如果存在一些确实很奇怪的原因和欲望,就会造成并加剧世界贫困,而事情会像乌龟一样费劲地浪费时间而不使用链接表吗?

Well you could create a linked table via the “transfer database” command. 好了,您可以通过“传输数据库”命令创建一个链接表。 It is only one extra line of code in front of the above code and then AGAIN the above two examples would work fine. 只需在上面的代码前面多一行代码,然后再次使用上面的两个示例就可以了。

However I see little if any advantage to creating + deleting a linked table. 但是,我看不到创建或删除链接表的优势。

I suppose for reasons of performance or perhaps for security or the legitimate reason of you not knowing the table ahead of time? 我想是出于性能方面的原因,还是出于安全性方面的考虑,或者出于合理原因,您不提前知道表格? Then I would suggest you use a saved a pass-though query as performance will be even faster. 然后,我建议您使用保存的直通查询,因为性能会更快。

So you can use this code: 因此,您可以使用以下代码:

Dim qdfPass       As DAO.QueryDef 
Set qdfPass = CurrentDb.QueryDefs("MyPass") 
qdfPass.SQL = "select * from dbo.MyTable;" 


CurrentDb.Execute "INSERT INTO LocalTable SELECT * FROM MyPass”

Note that the sql used in above qerydef MUST be native T-SQL and can be a view or even a store procedure like: 请注意,上述qerydef中使用的sql必须是本机T-SQL,并且可以是视图甚至是存储过程,例如:

qdfPass.SQL = "exec sp_myCoolStoreProc;" 

And the stored procedure can even be passed a parameter like this: 而且存储过程甚至可以传递如下参数:

qdfPass.SQL = "exec sp_myCoolStoreProc " & strMyParam

and then : 接着 :

CurrentDb.Execute "INSERT INTO LocalTable SELECT * FROM MyPass”

So we can even use a select into/append from a store procedure by doing the above and the table/sql server side is dynamic or can even be a stored procedure. 因此,通过执行上述操作,我们甚至可以在存储过程中使用select into / append方法,并且表/ sql服务器端是动态的,甚至可以是存储过程。 Again VERY little code. 再次非常小的代码。

I would suggest you avoid the idea proposed here to write recordset looping code unless one really has the desire to write looping code when none is required. 我建议您避免此处提出的编写记录集循环代码的想法,除非有人真的希望在不需要循环代码的情况下编写循环代码。 And things like PK would have to be dealt with separate in code if you use such loops since the local pk column may need to be skipped (you simply leave that column out of the select SQL). 如果使用这样的循环,则类似PK之类的事情必须在代码中单独处理,因为可能需要跳过本地pk列(您只需将该列排除在选择SQL之外)即可。

Note again that the connection string saved for the pass-though query does NOT require the user ID and password by using the above link showing how to “logon” to SQL Server. 再次注意,通过使用上面显示如何“登录” SQL Server的链接,为通过查询保存的连接字符串不需要用户ID和密码。 And if the table is known, then again a saved table link or pass-though query will suffice here. 而且,如果该表是已知的,则此处再次保存一个表链接或通过查询就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM