我找到了有关该主题的几篇文章,但问题有所不同。我正在尝试使用VB.net应用程序将数据从Excel 2016文件导入SQL Server 2017,以便最终用户无需在其计算机上安装SQL Server。我在调试模式下运行该应用程序以识别问题。
我再次阅读了与以下错误相关的各种实例的文章:
“ Microsoft.ACE.OLEDB.15.0”提供程序未在本地计算机上注册
我的代码:
Dim TheData As DataTable = GetExcelData(OpenFileDialog1)
Private Function GetExcelData(File As OpenFileDialog) As DataTable
Dim oleDbConnection As New OleDbConnection
Try
OpenOLEDBConnections(oleDbConnection, "Provider=Microsoft.ACE.OLEDB.15.0;Data Source=" & File.InitialDirectory & _
File.FileName & "; ;Extended Properties=""Excel 12.0 XML;HDR=YES;""")
Dim oleDbDataAdapter As OleDbDataAdapter = New OleDbDataAdapter("Select '' as ID, 'AL' as Supplier, LTRIM(RTRIM(GRADE)) AS GRADE, [ship date] as shipdate, LTRIM(RTRIM(coil)) AS coil, L15*1000 AS L15, H15*1000 AS H15 FROM [sheet1$] where COIL is not null group by GRADE, [SHIP DATE], COIL, L15, H15", oleDbConnection)
Dim dataTable As DataTable = New DataTable()
oleDbDataAdapter.Fill(dataTable)
oleDbConnection.Close()
Return dataTable
Catch ex As Exception
oleDbConnection.Close()
Return Nothing
End Try
End Function
Private Sub OpenOLEDBConnections(ByRef cnData As OleDbConnection, cnDataConstr As String)
If Not cnData.State = ConnectionState.Closed Then cnData.Close()
If cnData.State = ConnectionState.Closed Then
cnData.ConnectionString = cnDataConstr
cnData.Open()
End If
End Sub
答案 0 :(得分:0)
这不是灵丹妙药,但我不喜欢您的连接字符串的外观。似乎有一个“;”空间不足。试试这个
Dim XLSFile As String = "C:\DATA\test.xlsx"
Dim connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;" &
"Data Source=" & XLSFile & ";Extended Properties=""Excel 12.0 Xml;HDR=YES;"""
答案 1 :(得分:0)
尝试通过以下链接安装ACE提供程序
https://www.microsoft.com/en-us/download/confirmation.aspx?id=13255
如何将Excel导入SQL Server,请查看
https://www.red-gate.com/simple-talk/dotnet/c-programming/office-development-in-visual-studio/
或尝试使用此代码段
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Runtime.InteropServices
Imports Excel = Microsoft.Office.Interop.Excel
Public Module Program
Public Sub Main()
Dim officeType = Type.GetTypeFromProgID("Excel.Application")
If officeType Is Nothing Then
Console.WriteLine("Sorry, Excel must be installed!")
Console.WriteLine("Press any key to exit")
Console.ReadLine()
Return
End If
Const fileToRead As String = "C:\TMP\FirstTest.xlsx"
Dim xlApp As Excel.Application = Nothing
Dim workbooks As Excel.Workbooks = Nothing
Dim xlWorkBook As Excel.Workbook = Nothing
Dim sheets As Excel.Sheets = Nothing
Dim t1 As Excel.Worksheet = Nothing
Try
xlApp = New Excel.Application()
Console.WriteLine($"Trying to open file {fileToRead}")
workbooks = xlApp.Workbooks
xlWorkBook = workbooks.Open(fileToRead, 0, True, 5, "", "", True, Origin:=Excel.XlPlatform.xlWindows, Delimiter:=vbTab, Editable:=False, Notify:=False, Converter:=0, AddToMru:=True, Local:=1, CorruptLoad:=0)
sheets = xlApp.ActiveWorkbook.Sheets
Dim dic = New List(Of String)()
For Each mSheet In sheets
dic.Add($"[{t1.Name}$]")
Next
Using myConnection = New OleDbConnection($"Provider=Microsoft.Ace.OLEDB.12.0;Data Source={fileToRead};Extended Properties='Excel 12.0 Xml;HDR = YES;'")
Using dtSet = New DataSet()
For Each s In dic
Console.WriteLine($" Processing {s} table")
Dim myCommand = New OleDbDataAdapter($"select * from {s};", myConnection)
myCommand.TableMappings.Add("Table", s)
myCommand.Fill(dtSet)
Next
For Each t As DataTable In dtSet.Tables
Console.WriteLine($" Table {t.TableName} has {t.Rows.Count} records")
Next
End Using
End Using
dic = Nothing
Console.WriteLine("Successfully imported!")
Console.WriteLine("After closing Console Windows start Task Manager and be sure that Excel instance is not there!")
Console.WriteLine("Press any key to exit")
Console.ReadLine()
Catch e As Exception
Console.WriteLine($"Error importing from Excel : {e.Message}")
Console.ReadLine()
Finally
GC.Collect()
GC.WaitForPendingFinalizers()
If sheets IsNot Nothing Then
Marshal.FinalReleaseComObject(sheets)
sheets = Nothing
End If
xlWorkBook.Close()
If xlWorkBook IsNot Nothing Then
Marshal.FinalReleaseComObject(xlWorkBook)
xlWorkBook = Nothing
End If
xlApp.Quit()
If xlApp IsNot Nothing Then
Marshal.FinalReleaseComObject(xlApp)
xlApp = Nothing
End If
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
End Sub
End Module
答案 2 :(得分:0)
花了一天半的时间之后,我设法通过以下链接解决了该问题,并尝试了 Manju K Gowda提到的选项,2012年5月14日,星期一,下午12:14 Microsoft.ACE.OLEDB.15.0,并且还将我的连接字符串提供程序从OLEDB 15.0修改为12.0。
准确地说,在以调试或发布模式构建解决方案时,请将配置管理器保持在x86而不是AnyCPU上