tags:

views:

50

answers:

3

How can I perform queries on access using the C#? I want to create tables, and Insert/Select data from my access database.

+1  A: 

You should check out all things you can do with OdbcConnection and OdbcCommand.

You can even steal the Connection String for your connection from:

Access 2007 Connection String Samples

...that should be enough to get you started.

Justin Niessner
I usually use OleDbConnection and OleDbCommand for Access.
kbrimington
I`m using OleDbConnection, how can I create tables?
sikas
+2  A: 

Here's a tutorial to get you started.

http://www.csharphelp.com/2006/01/ms-access-application-with-c/

Depending on your version of Access, you may want to check out differenc connection strings as well.

http://connectionstrings.com

kbrimington
+1 because "http://connectionstrings.com" is your friend
Terrance
+2  A: 

Here are 2 pretty good starting tutorials

Here is a good intro into what is actually going on.
Here has some pretty helpful example code.

Protip: Make sure you have the correct ODBC Drivers installed if they are not already. I felt SOOOO stupid for not figuring that out from the start lol ;p

As far as dealing with you db assuming your not creating a access db on the fly all you would have to do is create your db in access, save it, and add it as a data source to your application.See here

Example Insert using second example:

OleDbCommand cmd = Myconnection.CreateCommand();

cmd.CommandText = "insert into familytree (firstname, lastname, city, Tel, Email)"+
values('foo','foo','foo',6666666,'foo')";

//used for operatations that dont require a data reader IE Insert statement
cmd.ExecuteNonQuery();

Same thing applies if you were wanting to create a table. just write your create table statement. see here for example and execute. But as far as common approaches go you generally want to have you table structures already set up for most simple apps and let your Application handle inserts, updates, and possibly deletes. Not saying you shouldn't do it that way but I would consider KISS whenever possible.

Oh and here is an msdn ref to the OleDbCommand class if you were wondering else you can do.
OleDbCommand

Terrance
+1 for the Protip. This is especially true in 64-bit environments!
kbrimington
that is great for retrieving the data, but what about creating the tables and inserting data?
sikas