Import query results into tool through VB.NET
I am writing a tool to import a database query results into Excel in VB.NET
I tried the following code. It is not working.
With objApp.ActiveSheet.QueryTables.Add(Connection:="ODBC;DSN=Build_statistics;", _
Destination:=objApp.Range("G15"))
.CommandText = "SELECT * from mytable"
.Name = "Query"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = Microsoft.Office.Interop.Excel.XlCellInsertionMode.xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
**.Refresh(BackgroundQuery:=False)** 'I am getting error here
End Wit开发者_JS百科h
It is working fine in VBA, but not in VB.NET.
Why not load the query results into a DataSet and then import the contents of the DataSet table into Excel?
The line
**.Refresh(BackgroundQuery:=False)** 'I am getting error here
is not correct and should be replaced by
.Refresh BackgroundQuery:=False
.
精彩评论