当前位置: 源码素材网 » VB教程 » 详情页

VB 使用SQL Insert语句批量插入数据的代码

  •   时间:2020-02-11
  • 概述:批量插入

VB 利用Insert语句批量插入数据,可命名测试数据库为db_Test.mdb,然后使用Set Rs = Con.Execute("insert into tb_laborage (员工姓名,所属部门,联系电话) values('" + Adodc1.Recordset.Fields("员工姓名") + "','" + Adodc1.Recordset.Fields("所属部门") + "','" + Adodc1.Recordset.Fields("联系电话") + "')")
Adodc1.Recordset.MoveNext,批量将数据添加到数据库中:

Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim Con As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Private Sub Command1_Click()
   On Error Resume Next
     Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db_Test.mdb;Persist Security Info=False"
     Con.Open
       Adodc1.RecordSource = "select * from tb_employee order by 编号"
       Adodc1.Refresh
       Adodc1.Recordset.MoveFirst
     For i = 0 To Adodc1.Recordset.RecordCount
         Set Rs = Con.Execute("insert into tb_laborage (员工姓名,所属部门,联系电话) values('" + Adodc1.Recordset.Fields("员工姓名") + "','" + Adodc1.Recordset.Fields("所属部门") + "','" + Adodc1.Recordset.Fields("联系电话") + "')")
         Adodc1.Recordset.MoveNext
     Next i
     
     Con.Close
     Adodc2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db_Test.mdb;Persist Security Info=False"
     Adodc2.RecordSource = "select * from tb_laborage order by 编号"
     Adodc2.Refresh
     Set DataGrid2.DataSource = Adodc2
     DataGrid2.Columns(0).Visible = False
End Sub

Private Sub Command2_Click()
  End
End Sub

Private Sub Form_Load()
  Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db_Test.mdb;Persist Security Info=False"
  Adodc1.RecordSource = "select * from tb_employee order by 编号"
  Adodc1.Refresh
  Adodc2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db_Test.mdb;Persist Security Info=False"
  Adodc2.RecordSource = "select * from tb_laborage order by 编号"
  Adodc2.Refresh
    Set DataGrid1.DataSource = Adodc1
    Set DataGrid2.DataSource = Adodc2
End Sub

    相关声明:

      若“VB 使用SQL Insert语句批量插入数据的代码”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。