Importing data from Access using ADODB References

John 506 Reputation points
2023-09-18T00:28:11.98+00:00

I need help with this to import the data successfully from an MDB file to a Data Grid View control

by using the ADODB and Access Interop assembly references.

I have this code snippet below:

using System; 
using System.Windows.Forms; 
using Microsoft.Office.Interop.Access.Dao;   
namespace Import_Data_001 {     
public partial class Form1 : Form     
{         
DBEngine dbEngine;         
Workspace workSpc;         
Database db;         
ADODB.Recordset rSet;          
public Form1()         
{             
InitializeComponent();         
}          
private void Form1_Load(object sender, EventArgs e, string name)         
{             
//This entire code runs well, but no data has been imported due to logical errors.              name = "@C:\\Users\\johnc\\OneDrive\\Documents\\Names.mdb";              
DBEngine dBEngine = new DBEngine();             
Workspace workSpc = dbEngine.Workspaces[0];             
Database db = workSpc.OpenDatabase(name);              
ADODB.Recordset rSet = (ADODB.Recordset)db.OpenRecordset("First Names");              
while (rSet != null)             
{                 
this.dataGridView1.DataSource = rSet.Fields[1].Value;                    
rSet.MoveNext();             
}              
rSet.Close();             
db.Close();             
workSpc.Close();         
}      
}} 
Developer technologies | Visual Studio | Other
Microsoft 365 and Office | Access | For business | Windows
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-09-18T08:40:00.04+00:00

    Hi @John , Welcome to Microsoft Q&A,

    The official document states ODBCDirect workspaces are not supported in Microsoft Access 2013. Use ADO if you want to access external data sources without using the Microsoft Access database engine..

    If you have no special needs, you can use ado instead of Dao.

    In Ado, you can choose System.Data.Odbc and System.Data.OleDb.

    Oledb is specific to the Windows platform.

    They all have official examples for you to try: OleDb and Odbc.

    Generally speaking, VB users tend to use Microsoft.Office.Interop.Access.Dao.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.