Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use OLEDB to read AccessFile from Stream to DataSet

I Use Oledb to read an AccessFile(.accdb) to DataSet, I don't know about table names or columns, The regular implementation is:

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();
    }
  }
}
}

But I need to get Access File from Stream, And I can't Write it on the Disk temporary, so what is your suggestion?

I need This overload of GetAccessDB(Stream AccessFile)? I search and find This, but that's not clear for me, I need finally get DataSet by all Tables in Access File.

Does any one know about this?

like image 890
Saeid Avatar asked Oct 21 '22 19:10

Saeid


1 Answers

I don't know any api function in OleDb for work with in-memory databases. Maybe, could you install a RAMDisk?

like image 119
mnieto Avatar answered Oct 24 '22 11:10

mnieto