永发信息网

VB6.0如何实现将excel数据导入mssql数据库中

答案:2  悬赏:60  手机版
解决时间 2021-02-06 01:10
  • 提问者网友:感性作祟
  • 2021-02-05 15:24
我在VB6.0中的模块中加入了连接sql的语句,连接已经没有问题了,做登录系统和将sql的数据导入到excel中也没有问题,但就是不知如何实现将excel数据导入mssql数据库中,批量的导入,请高手指教下,谢谢了先!

Public SQL As String
Public rs As ADODB.Recordset
Public ConnStr As String
Public Conn As ADODB.Connection
Public Function Selectsql(SQL As String) As ADODB.Recordset

'Dim ConnStr As String '已公共申明
'Dim Conn As ADODB.Connection
'Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Set Conn = New ADODB.Connection
On Error GoTo MyErr:
ConnStr = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=******;Initial Catalog=wzglxt;Data Source=127.0.0.1" '这是连接SQL数据库的语句
Conn.Open ConnStr
rs.CursorLocation = adUseClient
rs.Open Trim$(SQL), Conn, adOpenDynamic, adLockOptimistic
Set Selectsql = rs
Exit Function
MyErr:
Set rs = Nothing
Set Conn = Nothing
MsgBox "系统出错,请联系开发人员怀哥,E-mail:slyjpsh@qq.com", vbInformation, "系统提示"
End Function

执行
Dim strSQL As String
CommonDialog1.Filter = "电子表格文件(.xls)|*.xls"
CommonDialog1.DialogTitle = "请选择要导入的文件"
CommonDialog1.ShowOpen
SQL = "INSERT INTO Family SELECT * FROM OpenRowSet('microsoft.jet.oledb.4.0','Excel 14.0;HDR=Yes;database=" & CommonDialog1.FileName & " ;','select * from [Sheet1$] ')"
Set rs = Selectsql(SQL)
Conn.Execute SQL, , adExecuteNoRecords
Conn.Close
Set Conn = Nothing
MsgBox "导入成功", vbExclamation + vbOKOnly
Exit Sub
最佳答案
  • 五星知识达人网友:杯酒困英雄
  • 2021-02-05 15:32
这里是我用的代码,估计对你有用:

'On Error Resume Next
Dim fileadd As String
CommonDialog1.ShowOpen
CommonDialog1.Filter = "xls文件(*.xls)|*.xls" '选择你要的文件
fileadd = CommonDialog1.FileName
If fileadd = "" Then Exit Sub
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Open(fileadd) '打开已经存在的EXCEL工件簿文件
xlApp.Visible = False ' = True '设置EXCEL对象可见(或不可见)
Set xlSheet = xlBook.Worksheets(1) '设置活动工作表
For R = 1 To 99999 '行循环
If LTrim(RTrim(xlBook.Worksheets(1).Cells(R, 1))) <> "" Then
sybw.Adodc3.Refresh
sybw.Adodc3.Recordset.Find "ShiGongBuWei_Name='" & LTrim(RTrim(xlBook.Worksheets(1).Cells(R, 1))) & "'"
If sybw.Adodc3.Recordset.EOF Then
sybw.Adodc3.Recordset.AddNew
sybw.Adodc3.Recordset!ShiGongBuWei_Name = LTrim(RTrim(xlBook.Worksheets(1).Cells(R, 1)))
sybw.Adodc3.Recordset!FenXiangGongCheng_ID = bb
sybw.Adodc3.Recordset.Update
sybw.Adodc3.Refresh
' Call log(MM_Users_NameTrue, "增加了施工部位", MM_Companys_ID)
Else
' MsgBox " 施工部位重复! ", vbOKOnly, "用户信息"
End If
Else
R = 99999 + 1
End If
Next R

xlApp.DisplayAlerts = False '不进行安全提示 '
Set xlSheet = Nothing '
Set xlBook = Nothing '
xlApp.Quit '
Set xlApp = Nothing
全部回答
  • 1楼网友:北方的南先生
  • 2021-02-05 16:14
如果excel模板与access文件在同一目录下,直接复制下面代码到excel中,并执行 sub 从access中读数() dim cnn as new adodb.connection dim mypath as string dim strsql as string mypath = thisworkbook.path & "\access.mdb" '你的access文件名 mytable = "表名" '你的access中表的名称 'on error goto errmsg with cnn .provider = "microsoft.jet.oledb.4.0" .connectionstring = "data source =" & mypath .open end with strsql = "select * from 表名 where 字段名=条件" '你要提取的字段(*为提所有) sheet1.range("a2:a10000") = "" '提出的数据存放在sheet1中的a2 sheet1.range("a2").copyfromrecordset cnn.execute(strsql) cnn.close set cnn = nothing exit sub errmsg: msgbox err.description, , "错误报告" err.clear on error goto 0 end sub 如果代码提示“子过程没定义”则要在vba中选上“mircosoft activex data object 2.0”或2.0以上的其中一个
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯