excel VBA上传SQL记录集

时间:2014-09-30 12:52:59

标签: sql-server sql-server-2012 sql-insert import-from-excel

我无法弄清楚如何使用Microsoft SQL Server 2012上传到SQL,尽管我已经找到了如何使用以下方法将记录集上传到Access:

acc.DoCmd.TransferSpreadsheet _
    acImport, 

SQL数据库表名为index,excel工作表名为index2,上传数据位于A2:Y2

到目前为止,这是我提出的......

Sub upload()

Dim cnn As ADODB.Connection
Dim strConnectionString As String
Dim sh As Worksheet
Dim strInsert As String
Dim lngRow As Long

 Set sh = Sheets("index")
 Set cnn = New ADODB.Connection

 strConnectionString = "Provider=TRUNCATED" '

 'Start
 cnn.Open strConnectionString

 strInsert = "INSERT INTO index (Qnumber, Margin) VALUES (" & -rs2.Field(Sheets("index2").Range("a1")).Value & ", " & rs2.Field(Sheets("index2").Range("a2")).Value & ");"
 Debug.Print strInsert
 CurrentDb.Execute strInsert, dbFailonerror

0 个答案:

没有答案