首页 > 数据库 >vba-将excel中数据插入数据库

vba-将excel中数据插入数据库

时间:2022-12-04 20:11:05浏览次数:39  
标签:Dim vba Set End rs 数据库 excel sql con

Sub addDate1()
    Dim i As Integer, j As Integer, n As Integer
    Dim sql As String
    Dim con As New ADODB.Connection
   
    With con
        .Provider = "microsoft.ace.oledb.12.0"
        .ConnectionString = ThisWorkbook.Path & "\test.accdb"
        .Open
    End With
    
    Set rs = con.OpenSchema(adSchemaTables)
    n = Range("A1").End(xlDown).Row
    
    For i = 1 To n
       Set rs = New ADODB.Recordset
       sql = "select * from m_check"
       rs.Open sql, con, adOpenKeyset, adLockOptimistic
 
        rs.AddNew
        For j = 1 To rs.Fields.Count
            rs.Fields(j - 1) = Cells(i, j).Value
        Next j
        rs.Update
    Next i
    MsgBox "success" + Str(n)
    rs.Close
    con.Close   '关闭连接
    Set con = Nothing '释放变量
    Set rs = Nothing
 
End Sub

方法二,转换成数组

Sub addDate()
    Dim arr, i As Integer, j As Integer
    Dim sql As String
    arr = Range("A2").CurrentRegion
    
    Dim con As New ADODB.Connection
    Dim rs As New ADODB.Recordset
 
    With con
        .Provider = "microsoft.ace.oledb.12.0"
        .ConnectionString = ThisWorkbook.Path & "\test.accdb"
        .Open
    End With
 
    sql = "select * from m_check"
    rs.Open sql, con, adOpenKeyset, adLockOptimistic
    
    For i = 2 To UBound(arr)
        rs.AddNew
        For j = 1 To rs.Fields.Count
            rs.Fields(j - 1) = arr(i, j)
        Next j
        rs.Update
    Next i
    MsgBox "success"
     
'    sql = "delete * from m_check"
'    con.Execute (sql)  
    rs.Close
    con.Close   '关闭连接
    Set con = Nothing '释放变量
    Set rs = Nothing
End Sub

 

标签:Dim,vba,Set,End,rs,数据库,excel,sql,con
From: https://www.cnblogs.com/Insist-Y/p/16950574.html

相关文章

  • vba-在特定列中查询目标字符串是否存在
    PrivateSubSearchByTaget()IfConfigSht.Range("A:A").Find(What:="1232",LookAt:=xlWhole)IsNothingThenMsgBox"不存在"EndIfEndSubFunctionSea......
  • vba-加数据验证
    PrivateSubCommandButton1_Click()'给商品代码下拉列表赋值DimtbAsListObjectSettb=Sheet1.ListObjects("表_入库")DimS01AsObjectSetS......
  • 2.操作数据库(MySql中语句不区分大小写)
    操作数据库→操作数据库中的表→操作数据库中表的数据1.操作数据库1.创建数据库CREATEDATABASE[IFNOTEXISTS]westos--[]代表可选的,写不写都行2.删除数据库DRO......
  • 第十周内容回顾(包含部分数据库)
    目录python操作MySQLSQL注入问题视图触发器事务存储过程函数索引的相关概念索引数据结构慢查询优化前端之HTMLHTTP协议HTML概览head内常见标签body内基本标签常见符号body......
  • vba-获取某一个sheet某一列最大行
    FunctiongetLastRow(ByValsheetNameAsWorksheet,ByValcolumnNameAsString)AsLongDimmaxRowAsLongIfRight(rptfile,3)="xls"Thenmax......
  • 第17节-MySQL数据库备份与恢复
    1、备份1.1、备份多张表mysqldump-uroot-prootcjglscorecourse>score_course.sql1.2、备份多个数据库mysqldump-uroot-proot--databasescjglschool>......
  • python(7):python连接mysql数据库并获取数据
    python连接mysql数据库下载pymysql:pipinstallpymysql编写代码:importpymysqlclassMysqlUtils:def__init__(self):self.conn_obj=pymysql.conne......
  • Excel二维码图片生成器
    Excel二维码图片生成器它可以将excel文件的数据,每行数据生成一张二维码图片,并保存到电脑。软件无需安装,解压后即可直接使用,无需联网,操作简便快捷。软件下载地址:点此下载......
  • 10记录数据库
    数据库:DB(DataBase)参考链接1.概念顾名思义,就是数据仓库作用:存储、管理数据2.分类1.关系型数据库:SQL(StructuredQueryLanguage)MySQL、Oracle、SQLServer、SQLlite......
  • Redis安装配置与Jedis访问数据库
    目录一、NOSQL概要二、Redis概要2.1、相关网站三、安装与配置Redis3.1、下载最新版的Redis3.2、添加环境变量3.3、启动服务器3.4、启动客户端3.5、测试并运......