|
楼主 |
发表于 2013-7-20 17:35
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
游戏辅助 发表于 2013-7-20 13:52
我现在已决定只要实现最简单的功能就行,第一,链接sqlserver数据库,第二,清空对应数据表,第三,把exc ... - Sub qqqQQQ1()
- Dim conn As New ADODB.Connection
- Dim conn2
- Dim rs As New ADODB.Recordset
- Dim i%, strTemp$, RowNum%, K%
- Dim wkSheet As Worksheet
- If MsgBox("确认要导入数据吗?", vbYesNo) = 7 Then Exit Sub
- '建立与SQL的连接
- conn.Open "Driver={SQL Server};server=.;uid=sa;pwd=dz;database=mainlogsql_cq;"
- Set wkSheet = Worksheets("sheet2")
- strTemp = "DELETE FROM JP_JXSJB"
- strTemp = "WHERE WID='HH12P110'"
- RowNum = Range("c65536").End(xlUp).Row
- On Error GoTo 9
- conn.BeginTrans
- K = 0
- For i = 2 To RowNum
- If wkSheet.Cells(i, 2).Value <> 0 Then
- '拼写INSERT语句的SQL语句
- strTemp = "insert into JP_JXSJB(WID,SKNO,RID,DATE,TIME,ACTC,JXLX,XH,DEPTH,XDD,XDF,FWD,FWF,VDEPTH,X,Y,ZWY,ZFW,QJBHL,CLSJ,BZ) "
- strTemp = strTemp & " values( '" & wkSheet.Cells(i, 1).Value & "' , '" & _
- wkSheet.Cells(i, 2).Value & "' , '" & _
- wkSheet.Cells(i, 3).Value & "' , '" & _
- wkSheet.Cells(i, 4).Value & "' , '" & _
- wkSheet.Cells(i, 5).Value & "' , '" & _
- wkSheet.Cells(i, 6).Value & "' , '" & _
- wkSheet.Cells(i, 7).Value & "' , '" & _
- wkSheet.Cells(i, 8).Value & "' , '" & _
- wkSheet.Cells(i, 9).Value & "' , '" & _
- wkSheet.Cells(i, 10).Value & "' , '" & _
- wkSheet.Cells(i, 11).Value & "' , '" & _
- wkSheet.Cells(i, 12).Value & "' , '" & _
- wkSheet.Cells(i, 13).Value & "' , '" & _
- wkSheet.Cells(i, 14).Value & "' , '" & _
- wkSheet.Cells(i, 15).Value & "' , '" & _
- wkSheet.Cells(i, 16).Value & "' , '" & _
- wkSheet.Cells(i, 17).Value & "' , '" & _
- wkSheet.Cells(i, 18).Value & "' , '" & _
- wkSheet.Cells(i, 19).Value & "' , '" & _
- wkSheet.Cells(i, 20).Value & "' , '" & _
- wkSheet.Cells(i, 21).Value & "')"
- '执行INSERT语句
- conn.Execute strTemp
- K = K + 1
- End If
- Next
- conn.CommitTrans '结束插入操作的事务
- ThisWorkbook.Save
- '显示已经插入的记录条数
- conn.Close '关闭链接
- Set rs = Nothing '释放内存
- Set conn = Nothing '释放内存
-
- Set wkSheet = Nothing
- Application.ScreenUpdating = True
-
- MsgBox "导入完毕!" & Chr(10) & Chr(10) & "已经插入的条数:" & K
- Exit Sub
- 9:
- 'Conn.RollbackTrans
- MsgBox Err.Description & "第" & i & "行 导入出错"
- End Sub
复制代码 代码如上。 |
|