简体   繁体   English

MS Access / VB.Net - 如何将Excel工作表数据中的行插入MS Access表?

[英]MS Access/VB.Net - how can I insert rows from an Excel sheet data into MS Access table?

My program is written in VB.NET and I have an excel sheet with only one column and type of column is Text. 我的程序是用VB.NET编写的,我有一个excel表,只有一列,列的类型是Text。 Now I want to add all the rows of excel sheet into MS Access table. 现在我想将所有excel表的行添加到MS Access表中。

My Access table has 5 columns, and I have to provide some static data for rest of fields. 我的Access表有5列,我必须为其余字段提供一些静态数据。 I can't let them empty as my table structure won't allow me to let them empty. 我不能让他们空着,因为我的桌子结构不允许我让他们空着。 they are mandatory. 他们是强制性的。

My algorithm works this way 我的算法以这种方式工作

  • Read excel sheet data into a DataTable 将Excel工作表数据读入DataTable
  • Loop to the end of all rows of DataTable 循环到DataTable的所有行的末尾
  • Append text of each cell and some of my static information to make a query string 附加每个单元格的文本和一些静态信息以生成查询字符串

     INSERT into TableName(c1,c2,c3) values (v1,v2,v3), (v1,v2,v3), (v1,v2,v3), (v1,v2,v3) ... ... (n1,n2,n3); 
  • Execute this query 执行此查询

But I got error in this query, Any suggestions? 但是我在这个查询中遇到错误,有什么建议吗?

You can run SQL against a connection to interact directly with Access and Excel. 您可以针对连接运行SQL以直接与Access和Excel交互。 For example, working with the connection: 例如,使用连接:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Z:\Test.xlsm;
Extended Properties="Excel 12.0 xml;HDR=Yes;";

Or Jet 或喷射

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z:\Test.xls;
Extended Properties="Excel 8.0;HDR=Yes;";

You can run the following SQL: 您可以运行以下SQL:

INSERT INTO [;DATABASE=Z:\Docs\Test.accdb].Table1 (ID,Atext) 
SELECT ID, AText FROM [Sheet7$] 

Or Jet 或喷射

INSERT INTO [;DATABASE=Z:\Docs\Test.mdb].Table1 (ID,Atext) 
SELECT ID, AText FROM [Sheet7$] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据插入VB.net中的2个不同表中,我正在使用MS Access作为数据库 - How can I insert data into 2 different table in VB.net, I'm using MS Access as my db 无法从VB.NET插入MS Access数据库 - Cannot insert into MS Access database from VB.NET VB.NET/SQL用于删除MS Access数据库中的数据库行 - VB.NET / SQL for deleting database rows in a MS Access database vb.net MS Access使用SQL查询将具有自动编号主键列的记录插入表 - vb.net MS Access insert record with auto number primary key column into table with SQL query 在VB.net中使用while循环打印MS ACCESS表 - Print MS ACCESS table with while loop in VB.net VB.net使用BindingSource从DataGridView更新MS Access - VB.net Update MS Access from DataGridView with BindingSource 无法从 vb.net 更新 MS Access 数据库 - Cant update MS Access Database from vb.net SELECT *来自表格,如文本框。 (vb.net 2008 + ms access 2003) - SELECT * from table like from text box. (vb.net 2008 + ms access 2003) ASP.NET-VB.NET-从MS-Access数据库获取数据,其中用户名=? - ASP.NET - VB.NET - Getting data from MS-Access database where Username =? 如何将基于行的上一个和下一个记录数据插入到 MS Access 时间序列数据中? - How can I insert rows based previous and next record data into MS Access time sequenced data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM