从VBA执行SQL查询 - 运行时错误

时间:2015-03-26 10:36:48

标签: sql-server vba excel-vba excel

我尝试使用ADO在SQL-Server上从VBA执行最简单和最短的查询。我做错了什么?

Sub VBA_to_SQL()
    Dim cnn As ADODB.Connection
    Dim ConnectionString As String
    Set cnn = New ADODB.Connection

    cnn.ConnectionString = "driver={SQL Server};server=MyServerName;Database=MyDataBaseName;Trusted_Connection=yes;"
    cnn.Open

    If cnn.State = adStateOpen Then
       Set rs = cnn.Execute("begin transaction insert_value; delete from dbo.MyTable;COMMIT TRANSACTION insert_value;")
        cnn.Close
        MsgBox "Got through!"
    Else
        MsgBox "Sorry. No way today."
    End If
End Sub

我一直在第Set rs = cnn.Execute("begin transaction insert_value; delete from dbo.MyTable;;COMMIT TRANSACTION insert_value;")行收到错误 错误消息是运行时错误'-2147217908(80040e31)'

如果我评论错误导致的行,我设法通过服务器。因此建立连接,但不执行查询。

更新这段代码运行了好几天,突然停了下来。太糟糕了!

1 个答案:

答案 0 :(得分:0)

我找到了蠕虫,并将它碾碎了。在SQL Server上运行此帮助。谢谢大家的关心。

ALTER DATABASE [MyDataBaseName] 
SET RECOVERY SIMPLE;
CHECKPOINT;

DBCC SHRINKFILE (MyDataBaseName_log,0);

ALTER DATABASE [MyDataBaseName]
MODIFY FILE (NAME=MyDataBaseName_log,SIZE=8000MB,MAXSIZE=UNLIMITED,FILEGROWTH=1000MB);
ALTER DATABASE [MyDataBaseName] 
SET RECOVERY FULL;