How do I run a query that makes table in Access through C#?
I need to run query called "MyQuery" that I have in开发者_StackOverflow中文版 Access that makes a table.
How do I run this query in C# code ?
Have a look at this thread on vbCity, which seems to be exactly about your problem.
Your code might then look similar to this:
using System.Data;
using System.Data.
using (IDbConnection conn = new OleDbConnection(...)) // <- add connection string
{
conn.Open();
try
{
IDbCommand command = conn.CreateCommand();
// option 1:
command.CommandText = "SELECT ... FROM MyQuery";
// option 2:
command.CommandType = CommandType.TableDirect;
command.CommandText = "MyQuery";
// option 3:
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "MyQuery";
using (IDataReader reader = command.ExecuteReader())
{
// do something with the result set returned by reader...
}
}
finally
{
conn.Close();
}
}
精彩评论