简体   繁体   English

从 VBA 到 SQL 服务器的数据传输不完整

[英]Incomplete Data Transfer from VBA to SQL Server

I have a SQL-Server in Azure and want to transfer data from Excel into it using VBA through a VPN Tunnel.我在 Azure 中有一个 SQL-Server,我想使用 VBA 通过 VPN 隧道将数据从 Excel 传输到其中。 It works but out of 801 entries sometimes 320 are transmitted sometimes 324 but always in a range between 300 and 350 never more.它可以工作,但在 801 个条目中,有时会传输 320 个,有时会传输 324 个,但始终在 300 到 350 之间的范围内,再也不会。 They are transfer in the correct order as they are in the excel file.它们以正确的顺序传输,因为它们在 excel 文件中。 I am not getting an error at all.我根本没有收到错误。 Just saw in the SQL Table that there are not all entries in there.刚刚在 SQL 表中看到,其中并非所有条目。 I tried it some times and colleagues of my as well with the same result.我和我的同事也试过几次,结果也一样。 My code is:我的代码是:

Sub Importer(ByVal tableName As String, ByVal importQuery As String, pass As String)

    Dim con As Object
    
    Set con = CreateObject("ADODB.Connection")
    
        con.Open _
                "Driver={ODBC Driver 17 for SQL Server};" & _
                "Server=tcp:myServer,1433;" & _
                "Database=myDB;" & _
                "Uid= admin;" & _
                "Pwd=" & pass & ";" & _
                "TrustServerCertificate=no;" & _
                "Connection Timeout=30;" & _
                "Encrypt=yes;"
        con.Execute importQuery
        con.Close
       
    Set con = Nothing
    
End Sub

While importQuery contails 801 INSERT INTO entries in one query looking like this:虽然 importQuery 在一个查询中包含 801 个 INSERT INTO 条目,如下所示:

INSERT INTO dbo.table(Variable1, Variable1, Variable1) Values(", 3)
INSERT INTO dbo.table(Variable1, Variable1, Variable1) Values(", 3)
INSERT INTO dbo.table(Variable1, Variable1, Variable1) Values(", 3)

... ...

I tried to set the timeout up but with no effect.我试图设置超时但没有效果。 Anyone with an idea what might cause the problem?任何人都知道可能导致问题的原因是什么?

Your pseudo-code is syntactically incorrect, but that's irrelevant I guess.您的伪代码在语法上不正确,但我想这无关紧要。

Currently you're doing multiple singleton INSERT statements in an non-transactional manner.当前,您正在以非事务方式执行多个 singleton INSERT语句。 Surely your connection is being broken or some other issue happens in the middle of the code executing, resulting in only some of the data being inserted.肯定是您的连接断开了,或者在代码执行过程中发生了一些其他问题,导致仅插入了部分数据。

Either wrap a transaction around the entire set of INSERT statements.要么围绕整个INSERT语句集包装一个事务 Or even simpler would be to convert them to a single INSERT statement of multiple values, which will be implicitly transactional and potentially more efficient, like so:或者更简单的方法是将它们转换为包含多个值的单个INSERT语句,这将是隐式事务性的并且可能更有效,如下所示:

INSERT INTO dbo.table(Column1, Column2, Column3)
VALUES
    (ValueA1, ValueA2, ValueA3),
    (ValueB1, ValueB2, ValueB3),
    (ValueC1, ValueC2, ValueC3),
    (ValueD1, ValueD2, ValueD3),
    (ValueE1, ValueE2, ValueE3);

As far as whatever error / issue your application is running into, unfortunately won't be advisable without more details such as a specific error message.至于您的应用程序遇到的任何错误/问题,不幸的是,如果没有更多详细信息(例如特定错误消息),这是不可取的。 But at least the fix I mentioned above will ensure either all or none of the data is consistently inserted.但至少我上面提到的修复将确保所有数据或没有数据被一致插入。

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

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