实现:
using System.Data;
using System.Data.OleDb;
private static string databaseFilePath = string.Format("{0}\\ISystem.mdb", System.IO.Directory.GetCurrentDirectory());
public static DataTable ReadTable(string tableName, string condition = "", string columns = "*")
{
DataTable dataTable = new DataTable();
string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", databaseFilePath);
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
string query = string.Format("SELECT {0} FROM {1} {2}", columns, tableName, condition);
using (OleDbCommand command = new OleDbCommand(query, connection))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
adapter.Fill(dataTable);
}
}
}
return dataTable;
}
调用 :
当条件为布尔型:
DataTable dataTableRoot = SystemDatabase.ReadGroupNameTable(string.Format("Where Active={0}",true),"GroupID,GroupName");
当条件为整数型:
DataTable dataTableNode = SystemDatabase.ReadCommandSetTable(string.Format("Where GroupID={0}", 2), "CommandName");
当条件为字符串型:
TestItemDatabase.ReadTestProcedureTable(string.Format("WHERE TIName='{0}'",openTestItemName),"Active,Label,TestCommand,Parameter,Comment,ParameterIdx");