我需要运行名为“MyQuery”的查询,我在Access中创建了一个表。
如何在C#代码中运行此查询?
答案 0 :(得分:3)
看看this thread on vbCity,这似乎与您的问题完全一致。
您的代码可能看起来与此类似:
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();
}
}