#region 连接本地数据库
private OleDbConnection con;
/// <summary>
/// 打开数据库连接
/// </summary>
/// <param name="DBpath">数据库路径(包括数据库名)</param>
private void Open(String DBpath)
{
if (con == null)
con = new OleDbConnection(@" Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =C:\Users\yuleyi\Desktop\LocalConfig.mdb");
if (con.State == ConnectionState.Closed)
con.Open();
}
/// <summary>
/// 创建一个命令对象并返回该对象
/// </summary>
/// <param name="sqlStr">数据库语句</param>
/// <param name="file">数据库所在路径</param>
/// <returns>OleDbCommand</returns>
private OleDbCommand CreateCommand(string sqlStr, string file)
{
Open(file);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlStr;
cmd.Connection = con;
return cmd;
}
/// <summary>
/// 执行数据库查询
/// </summary>
/// <param name="sqlStr">查询语句</param>
/// <param name="tableName">填充数据集表格的名称</param>
/// <param name="file">数据库所在路径</param>
/// <returns>查询的数据集</returns>
public DataSet GetDataSet(string sqlStr, string file)
{
DataSet ds = new DataSet();
OleDbCommand cmd = CreateCommand(sqlStr, file);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmd);
dataAdapter.Fill(ds);
cmd.Dispose();
dataAdapter.Dispose();
return ds;
}