Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

MS Access automation with C#

Posted on 2003-02-20
5
Medium Priority
?
541 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
  • 2
4 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

571 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