首页 > 代码库 > EXCEL页面数据快速写入SQL数据库
EXCEL页面数据快速写入SQL数据库
将EXCEL数据存入SQL表, 一万行记录大概5秒
Dim conn As New ADODB.ConnectionDim CNN As New ADODB.Connection‘Dim rst As New ADODB.RecordsetDim Sql As StringDim j, v As IntegerConst cnnstr = "Provider = SQLOLEDB;" & _"Data Source = ip;" & _"Initial Catalog = apsdb;User ID =sa;Password = pw;"conn.Open cnnstrconn.Execute "truncate table tjnpg"Application.EnableEvents = FalseOn Error GoTo ErrHandlej = ActiveSheet.Range("A65535").End(xlUp).Rowv = Application.VersionIf v = 11 Then ‘EXCEL2003CNN.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & ThisWorkbook.FullNameElseCNN.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=excel 12.0;Data Source=" & ThisWorkbook.FullNameEnd If‘ Sql = "select 部门,交期,产品号,生产订单号,旧物料号,物料描述 from [Sheet1$]"‘ rst.Open Sql, CNN, adOpenKeyset, adLockOptimistic‘ Debug.Print rst.RecordCountSql = "INSERT INTO [odbc;Driver={SQL Server};" & _"Server=ip;Database=apsdb;" & _"UID=sa;PWD=pw].tjnpg " & _"select 部门,交期,产品号,生产订单号,旧物料号,物料描述 from [Sheet1$]"CNN.Execute SqlMsgBox "存入成功.", vbInformation, "诚益资讯"‘关闭数据连接CNN.CloseSet rst = NothingSet CNN = Nothingconn.CloseSet conn = NothingGoTo ExitHandleExitHandle:Application.EnableEvents = TrueExit SubErrHandle:Application.EnableEvents = TrueMsgBox Err.Description, vbCritical, "诚益资讯"
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。