使用OLEDB读取AccessFile从流为DataSet [英] Use OLEDB to read AccessFile from Stream to DataSet

查看:112
本文介绍了使用OLEDB读取AccessFile从流为DataSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OLEDB来读取AccessFile(.ACCDB)到DataSet中,我不知道表名或列,常规的实现是:



 公共无效GetAccessDB(字符串文件路径){

this.ConnectionString =供应商= Microsoft.ACE.OLEDB.12.0;数据源=+文件路径;
//获取表名
this.TableNames =新的List<串GT;();
使用(System.Data.OleDb.OleDbConnection的OleDbConnection =新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行dt.Rows)
{
串strSheetTableName =行[TABLE_NAME]的ToString()。
如果(行[TABLE_TYPE]的ToString()==TABLE。)
this.TableNames.Add(strSheetTableName);
}
oledbConnection.Close();
}


this.Dataset =新System.Data.DataSet中();
使用(System.Data.OleDb.OleDbConnection的OleDbConnection =新System.Data.OleDb.OleDbConnection(this.ConnectionString))
{
的foreach(在this.TableNames字符串表)
{
字符串命令=的String.Format(SELECT * FROM {0};表);使用
(System.Data.OleDb.OleDbCommand CMD =新System.Data.OleDb.OleDbCommand(命令的OleDbConnection))
{
cmd.CommandType = System.Data.CommandType.Text;
oledbConnection.Open();
System.Data.OleDb.OleDbDataReader博士= cmd.ExecuteReader();
this.Dataset.Load(DR,System.Data.LoadOption.OverwriteChanges,表);
oledbConnection.Close();
}
}
}
}



不过,我需要从流得到Access文件,我不能把它写在磁盘暂时的,所以你有什么建议吗?



我需要 GetAccessDB(流AccessFile)的此重载
我搜索和查找,但是这尚不清楚对我来说,我需要最终在访问文件中的所有表得到的数据集。



是否有任何人知道这事吗?

解决方案

如果您在 MS SQL服务器控制,这是个好消息。我目前看到2选择:




  1. 创建的 CLR asssembly 将处理(异步是一个好主意),一旦将文件在上传的文件表进行。它会创建一个临时 MS访问通过上传文件的内容在服务器上的文件。然后,用 OLEDB 打开它,分析它,并插入来自它在与第一个表上传的文件记录所提取的信息映射SQL表中的信息。然后,你可以去看看在这第二个表中的数据


  2. 另一种选择是发送到SQL命令将执行以下操作:




    1. 使用上传的文件字节的创建文件系统的一个文件。

    2. <李>然后,使用文件作为链接服务器
    3. 使用 SELECT 来查询Access数据库




您可能已经注意到,这两个选项涉及创建SQL Server上的一个(至少暂时)文件。


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?

解决方案

If you have control over the MS SQL Server, that's good news. I currently see 2 alternatives:

  1. Create a CLR asssembly that will process (asynchronously is a good idea) the file once the insert is made in the uploaded files table. It would create a temporary MS Access file on the server by using the content of the uploaded file. Then, open it with OleDB, parse it and insert the information from it in a SQL table which maps the extracted information with the uploaded file record in the first table. Then, you could go and look for the data in this second table.

  2. Another option would be to send to the SQL a command which will do the following:

    1. Use the uploaded file bytes to create a file on the filesystem.
    2. Then, use the file as a linked server
    3. Use SELECT to query the Access database

You may have noticed that both options involve creating a (at least temporary) file on the SQL Server.

这篇关于使用OLEDB读取AccessFile从流为DataSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆