用VBA 实现 从EXCEL 读取数据 然后插入SQL 数据库 怎么实现

2024-11-06 09:13:40
推荐回答(2个)
回答1:

'sql完整例子
Sub testSql()
'定义连接对象
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset

'定义连接字符串
Dim conStr As String
Dim sqlstr As String
'连接字符串-以下是连接MSSQL数据库
conStr = "Provider=sqloledb; " _
& "Server=192.168.1.121; " _
& "Database=DATABASENAME;Uid=admin;Pwd=admin;"
cnn.Open conStr
sqlstr = "SELECT * from tablename"
rs.Open sqlstr, cnn
Range("a2").CopyFromRecordset rs
rs.Close
cnn.Close
End Sub

回答2:

引用ADO来实现