我一直在嘗試過去的幾天,我一直在做的是使用谷歌搜索和嘗試任何代碼,我可以找到並測試出來。我放棄了,希望這裏的聰明人能幫助我。新手試圖從excel插入數據到SQL Server 2008
我想從Excel工作表中的數據加載到SQL Server中已經存在的表。所有列和數據屬性都完全匹配。
當我運行該腳本,它提供了一個錯誤:
Run-time error '-2147217865 (80040e37)':
The Microsoft Access database engine could not find the object 'DATA_FOR_CUTTING'. Make sure the object exists and that you spell its name and the path correctly. If the 'DATA_FOR_CUTTING' is not a local object, check your network connection or contact the server administrator
這是腳本(我用Excel 2010中)
Sub Excel_To_SQL()
Dim con As ADODB.Connection
Dim con2 As New ADODB.Connection
Dim s As String
Dim s2 As String
Dim s3 As String
Dim WSP1 As Worksheet
Set con = New ADODB.Connection
Application.DisplayStatusBar = True
Application.StatusBar = "Contacting SQL Server..."
s = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\xxx\Documents\xxx\xxxx\Book2.xlsx';" _
& "Extended Properties=""Excel 8.0;HDR=Yes;IMEX=0;Readonly=False"";"
' 'HDR = yes - to exclude header of excel file upon reading
con.Open s
s2 = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=xxx;Password=xxx;Initial Catalog=xxx;Data Source=xxxxx;"
con2.Open s2
s3 = "INSERT INTO dbo.DATA_FOR_CUTTING SELECT * FROM [Data_for_Cutting$]"
con.Execute s3
con.Close
Set con = Nothing
Application.StatusBar = "Data successfully loaded."
End Sub
#
我固定的連接字符串和有沒有更多的錯誤,但它也無能爲力。它沒有讀取工作表的內容,所以沒有行被插入到sql表中。但是,如果我執行「INSERT INTO ... VALUES(XXX)」命令,則將VALUES插入到SQL表中,因此連接和命令正在工作。有人知道爲什麼它沒有閱讀工作表?謝謝。
是你的表存在嗎?檢查您提供的表名或您提供的工作表名稱 – 2015-02-24 04:27:58
您的電子表格是否有名爲Data_for_Cutting的工作表? – Mych 2015-02-24 10:40:00
我剛剛遇到這個http://epplus.codeplex.com/它看起來像這可以幫助...很多的例子。此外,你的錯誤提到Access DB,而你的narative說Excel到SQL 2008是你在s中使用錯誤的提供者。 – Mych 2015-02-24 11:01:22