首页 > 代码库 > EXCEL页面数据快速写入SQL数据库

EXCEL页面数据快速写入SQL数据库

将EXCEL数据存入SQL表, 一万行记录大概5秒
Dim conn As New ADODB.ConnectionDim CNN As New ADODB.ConnectionDim 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, "诚益资讯"
View Code