|
首先在VBE中选中菜单“工具”-“引用”,在弹出的对话框中,在“Microsoft ActiveX Data Objects 2.1 library”前打勾选中,也可以用2.1以上的版本。这样就建立了ADO的引用。
Sub insertData() Dim I%, strTemp$, strTemp2$, RowNum% Dim wkSheet As Worksheet I = MsgBox("确认要导入数据吗?", vbYesNo) If I = 7 Then Exit Sub End If Conn.Open "provider=sqloledb.1;persist security info=false;user id=用户名;pwd=口令;data source=192.168.2.2;initial catalog=首选数据库"
Application.ScreenUpdating = False Set wkSheet = Worksheets("Check") wkSheet.Activate I = 2 Do While wkSheet.Cells(I, 1).Value <> "" I = I + 1 Loop I = I - 1 RowNum = I
'开始倒数据
'直接插入数据表 Conn.BeginTrans For I = 2 To RowNum If wkSheet.Cells(I, 4).Value <> "" Then strTemp = "insert into tprice (colno,productname,spec,editdate,unit,price,provider) " strTemp = strTemp & " values('" & Trim((wkSheet.Cells(I, 7).Value)) & "','" strTemp = strTemp & Replace(wkSheet.Cells(I, 1).Value, "'", "''") & "','" & Replace(wkSheet.Cells(I, 2).Value, "'", "''") & "','" strTemp = strTemp & Format(Now, "yyyy-mm-dd hh:mm:ss") & "','" & Replace(wkSheet.Cells(I, 3).Value, "'", "''") strTemp = strTemp & "'," & wkSheet.Cells(I, 4).Value & ",'" & wkSheet.Cells(I, 5).Value & "')" Conn.Execute strTemp End If Next Conn.CommitTrans ThisWorkbook.Save
Conn.Execute "update tprice set tprice.colname=colno.colname from tprice,colno where tprice.colno=colno.colno and tprice.colname is null" Conn.Close Set Conn = Nothing Set wkSheet = Nothing Application.ScreenUpdating = True MsgBox "导入完毕!" End Sub |
评分
-
1
查看全部评分
-
|