我已经从Excel数据创建了一个记录集,但是我无法选择记录集并将数据插入到数据库中:
Sub insrt()
Dim xlXML As Object
Dim adoRecordset As Recordset
Dim rng As Range
Dim Conn_lyr
Dim ConnString_lyr
Dim Vsql_lyr As String
Range("B6").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Set rng = Selection
Set adoRecordset = CreateObject("ADODB.Recordset")
Set xlXML = CreateObject("MSXML2.DOMDocument")
xlXML.LoadXML rng.Value(xlRangeValueMSPersistXML)
adoRecordset.Open xlXML
ConnString_lyr = "DRIVER={SQL Server Native Client 11.0}; SERVER=xxxxxxxx;UID=xxxxxxx;PWD=xxxxxxx;"
Set Conn_lyr = CreateObject("ADODB.Connection")
Vsql_lyr = "insert into " + Range("E1").Value + ".DBO." + Range("f1").Value + " select * from adoRecordset"
With Conn_lyr
.ConnectionString = ConnString_lyr
.Open
.Execute (Vsql_lyr)
End With
Conn_lyr.Close
Set Conn8 = Nothing
End Sub