?
Solved

MS Access automation with C#

Posted on 2003-02-20
5
Medium Priority
?
535 Views
Last Modified: 2008-02-26
I am trying to use automation between a C# interface and MS Access.  It sounds like it would be easier than ADO.NET with an instance of Access running and using its forms and reports.  Microsoft has an example posted to try it out but even when you go through their example you get a build error that Access is already defined as an alias in your namespace.  I would appreciate a solution to this, an alternate info site, or a solution to automation of MS Access with C#.  Thanks
0
Comment
Question by:spikewolf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 

Author Comment

by:spikewolf
ID: 7994889
Nevermind, comment out the line that causes the error on Microsofts example, and it works fine.  Go figure.
0
 

Expert Comment

by:shade73
ID: 8001664
In regards to your question, I use OleDbConnection to connect to the database (just set the properties to be the Access database you want to connect to).  Then create an OleDbCommand and change the properties of Connection to be the access database you just created.  Now, you will also need to add the line

using System.Data;
using System.Data.OleDb;

Now in the section of the code right after InitializeComponents() you will add this line.

OleDbConnection1.Open(); // This opens your connection

now to do any sort of queries you will do a
OleDbCommand1.CommandText = "SELECT * FROM Table;";
// this code sets up the query

OleDbDataReader drt = OleDbCommand1.ExecuteReader();
while (drt.Read())
{
// this section is where you get all the stuff from the //table
}

to do a non-query is even easier, it's just:
OleDbCommand1.CommandText = "INSERT INTO Table VALUES ('blah');";
OleDbCommand1.ExecuteNonQuery();

That's how I do all of my work with Access.  I hope this helps you.
0
 

Expert Comment

by:shade73
ID: 8004055
In regards to your question, I use OleDbConnection to connect to the database (just set the properties to be the Access database you want to connect to).  Then create an OleDbCommand and change the properties of Connection to be the access database you just created.  Now, you will also need to add the line

using System.Data;
using System.Data.OleDb;

Now in the section of the code right after InitializeComponents() you will add this line.

OleDbConnection1.Open(); // This opens your connection

now to do any sort of queries you will do a
OleDbCommand1.CommandText = "SELECT * FROM Table;";
// this code sets up the query

OleDbDataReader drt = OleDbCommand1.ExecuteReader();
while (drt.Read())
{
// this section is where you get all the stuff from the //table
}

to do a non-query is even easier, it's just:
OleDbCommand1.CommandText = "INSERT INTO Table VALUES ('blah');";
OleDbCommand1.ExecuteNonQuery();

That's how I do all of my work with Access.  I hope this helps you.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 11698611
PAQed, with points refunded (100)

modulo
Community Support Moderator
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question