我使用 Oledb 将 AccessFile(.accdb) 读取到 DataSet,我不知道表名或列,常规实现是:
public void GetAccessDB(string filepath){
this.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = " + filepath;
// get Table Names
this.TableNames = new List<string>();
using (System.Data.OleDb.OleDbConnection oledbConnection = new System.Data.OleDb.OleDbConnection(this.ConnectionString))
{
oledbConnection.Open();
System.Data.DataTable dt = null;
dt = oledbConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
foreach (System.Data.DataRow row in dt.Rows)
{
string strSheetTableName = row["TABLE_NAME"].ToString();
if (row["TABLE_TYPE"].ToString() == "TABLE")
this.TableNames.Add(strSheetTableName);
}
oledbConnection.Close();
}
this.Dataset = new System.Data.DataSet();
using (System.Data.OleDb.OleDbConnection oledbConnection = new System.Data.OleDb.OleDbConnection(this.ConnectionString))
{
foreach (string table in this.TableNames)
{
string command = string.Format("SELECT * FROM {0};", table);
using (System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(command, oledbConnection))
{
cmd.CommandType = System.Data.CommandType.Text;
oledbConnection.Open();
System.Data.OleDb.OleDbDataReader dr = cmd.ExecuteReader();
this.Dataset.Load(dr, System.Data.LoadOption.OverwriteChanges, table);
oledbConnection.Close();
}
}
}
}
但是我需要从 Stream 中获取访问文件,并且我不能将它临时写入磁盘,所以你的建议是什么?
我需要这个超载GetAccessDB(Stream AccessFile)
?我搜索并找到了This,但这对我来说并不清楚,我最终需要通过 Access File 中的所有表获取 DataSet 。
有人知道吗?