excel錶鏈接MSSQL表

Private Sub CommandButton1_Click()

Dim cnn As New ADODB.Connection
Dim strsql As String
Dim rnum As Integer, i As Integer

'① excel+excel  鏈接
'cnn.Open "Provider=Microsoft.Jet.OleDb.4.0;Extended Properties=Excel 8.0;Data Source=" & ThisWorkbook.FullName
'strsql = "select * from [入庫明細$]"
'rst.Open strsql, cnn, adOpenKeyset


'② excel+sql  鏈接
cnn.Open "provider=sqloledb;server=127.0.0.1;database=mytest;uid=sa;pwd=;"

strsql = ""

rnum = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To rnum
    strsql = strsql & "insert into table1 values(" & Cells(i, 1) & " ,'" & Cells(i, 2) & "'," & Cells(i, 3) & ") "
    cnn.Execute strsql
    strsql = ""
Next i

cnn.Close
Set cnn = Nothing

End Sub
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章