C# 64 位模式不支持 OleDB?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/861022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:37:44  来源:igfitidea点击:

OleDB not supported in 64bit mode?

c#.netcsv64-bitoledb

提问by John Weldon

I've been using Microsoft.Jet.OLEDB.4.0 and Microsoft.ACE.OLEDB.12.0 to read in .csv, .xls, and .xlsx files.

我一直在使用 Microsoft.Jet.OLEDB.4.0 和 Microsoft.ACE.OLEDB.12.0 来读取 .csv、.xls 和 .xlsx 文件。

I just found out that neither of these technologies are supported in native 64bit mode!

我刚刚发现原生 64 位模式不支持这两种技术!

I have 2 questions:

我有两个问题:

  1. What is the supported way to programatically read .csv, .xls, and .xlsx files in 64 bit mode. I just can't find answers to this anywhere.

  2. If I can't read in all three file types, what is the best way to read in .csv files in a 64 bit environment?

  1. 在 64 位模式下以编程方式读取 .csv、.xls 和 .xlsx 文件的支持方式是什么。我只是无法在任何地方找到答案。

  2. 如果我无法读取所有三种文件类型,那么在 64 位环境中读取 .csv 文件的最佳方法是什么?

Notes:

笔记:

  • I'm using .NET (3.5p1)
  • This is a shrink wrap app; redistribution is a key factor.
  • 我正在使用 .NET (3.5p1)
  • 这是一个收缩包装应用程序;再分配是一个关键因素。

Update:

更新:

I can use CorFlags to force the application to run in 32bit mode, which works, but is not desirable.

我可以使用 CorFlags 强制应用程序在 32 位模式下运行,这可行,但不可取。

采纳答案by JP Alioto

Here is a discussion of what to do about deprecated MDAC. I am afraid the answer is not very satisfying ...

这里是关于如何处理已弃用的 MDAC的讨论。恐怕答案不是很令人满意……

These new or converted Jet applications can continue to use Jet with the intention of using Microsoft Office 2003 and earlier files (.mdb and .xls) for non-primary data storage. However, for these applications, you should plan to migrate from Jet to the 2007 Office System Driver. You can download the 2007 Office System Driver, which allows you to read from and write to pre-existing files in either Office 2003 (.mdb and .xls) or the Office 2007 (*.accdb, *.xlsm, *.xlsx and *.xlsb) file formats. IMPORTANT Please read the 2007 Office System End User License Agreement for specific usage limitations.

Note: SQL Server applications can also access the 2007 Office System, and earlier, files from SQL Server heterogeneous data connectivity and Integrations Services capabilities as well, via the 2007 Office System Driver. Additionally, 64-bit SQL Server applications can access to 32-bit Jet and 2007 Office System files by using 32-bit SQL Server Integration Services (SSIS) on 64-bit Windows.

这些新的或转换后的 Jet 应用程序可以继续使用 Jet,目的是将 Microsoft Office 2003 和更早版本的文件(.mdb 和 .xls)用于非主要数据存储。但是,对于这些应用程序,您应该计划从 Jet 迁移到 2007 Office System Driver。您可以下载 2007 Office System Driver,它允许您读取和写入 Office 2003(.mdb 和 .xls)或 Office 2007(*.accdb、*.xlsm、*.xlsx 和*.xlsb) 文件格式。重要信息请阅读 2007 Office System 最终用户许可协议以了解特定的使用限制。

注意:SQL Server 应用程序还可以通过 2007 Office System 驱动程序访问 2007 Office System 和更早版本的文件,这些文件也来自 SQL Server 异构数据连接和集成服务功能。此外,64 位 SQL Server 应用程序可以通过在 64 位 Windows 上使用 32 位 SQL Server 集成服务 (SSIS) 访问 32 位 Jet 和 2007 Office System 文件。

回答by Tim Jarvis

Actually I think Linq is your best solution for this.

实际上,我认为 Linq 是您最好的解决方案。

Something like....

就像是....

IEnumerable<MyObj> ObjList = GetObjList(yourCSVFileNAme);

var qry = from o in ObjList
          where o.MyField == Something
          select o;

and your GetObjList method looks something like

你的 GetObjList 方法看起来像

Public IEnumerable<MyObj> GetObjList(string filename)
{
  // Obvioulsly you would have some actual validation and error handling
  foreach(string line in File.ReadAllLines(filename))
  {
    string[] fields = line.Split(new char[]{','});
    MyObj obj = new MyObj();
    obj.Field = fields[0];
    obj.AnotherField = int32.Parse(fields[1]);
    yield return obj;
  }
}

回答by Stefan Rusek

The main problem is that the Jet DBMS is a 32bit library that gets loaded into the calling process, so you will never be able to use Jet directly from within your app in 64bit mode. As Tim mentioned you could write your own csv parser, but since this is a shrink-wrap app you want something that will handle a wider range of formats. Luckily, there are a number of ways to talk 32-bit apps, so you can still use Jet with a trick.

主要问题是 Jet DBMS 是一个 32 位库,它被加载到调用过程中,因此您永远无法在 64 位模式下直接从应用程序中使用 Jet。正如 Tim 提到的,您可以编写自己的 csv 解析器,但由于这是一个收缩包装应用程序,您需要一些可以处理更广泛格式的东西。幸运的是,有多种方法可以与 32 位应用程序进行交互,因此您仍然可以通过一个技巧来使用 Jet。

I would write a little exe that was marked to run only in 32-bit mode. This exe would take a command line argument of the name of the file to read and the name of a temp file to write to. I would use Jet to load the csv/xls, then put the data into an array of arrays, and use the xml serializer to write the data to the temp file.

我会编写一个标记为仅在 32 位模式下运行的小 exe。这个 exe 将接受一个命令行参数,即要读取的文件名和要写入的临时文件的名称。我会使用 Jet 加载 csv/xls,然后将数据放入数组数组中,并使用 xml 序列化程序将数据写入临时文件。

Then when I need to load/convert a csv/xls file, I would do the following:

然后,当我需要加载/转换 csv/xls 文件时,我会执行以下操作:

object[][] ConvertFile(string csvOrXlsFile)
{
    var output = System.IO.Path.GetTempFileName();
    try
    {
        var startinfo = new System.Diagnostics.ProcessStartInfo("convert.exe",
            string.Format("\"{0}\" \"{1}\"", csvOrXlsFile, output));

        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo = startinfo;

        proc.Start();
        proc.WaitForExit();

        var serializer = new System.Xml.Serialization.XmlSerializer(typeof(object[][]));
        using (var reader = System.IO.File.OpenText(output))
            return (object[][])serializer.Deserialize(reader);
    }
    finally
    {
        if (System.IO.File.Exists(output))
            System.IO.File.Delete(output);
    }
}

回答by Christopher G. Lewis

You could try the FileHelperslibrary for your flat-file parsing. Works amazingly well.

您可以尝试使用FileHelpers库进行平面文件解析。效果非常好。

回答by Joe Erickson

SpreadsheetGear for .NETcan read and write .csv / .xls / .xlsx workbooks (and more) and supports 64 bit .NET 2.0+. SpreadsheetGear can be distributed royalty free with your shrink wrap applications.

SpreadsheetGear for .NET可以读取和写入 .csv / .xls / .xlsx 工作簿(以及更多)并支持 64 位 .NET 2.0+。SpreadsheetGear 可以随您的收缩包装应用程序免费分发。

You did not specify whether your application is WinForms or ASP.NET but SpreadsheetGear works with either. You can see live ASP.NET (C# & VB) samples here, learn about the WinForms samples hereand download a free trial hereif you want to try it yourself.

您没有指定您的应用程序是 WinForms 还是 ASP.NET,但 SpreadsheetGear 可以使用任何一种。你可以看到现场ASP.NET(C#和VB)样本这里,了解的WinForms样品这里并下载免费试用版在这里,如果你想自己尝试一下。

Disclaimer: I own SpreadsheetGear LLC

免责声明:我拥有 SpreadsheetGear LLC

回答by neo

You can use Microsoft Access Database Engine 2010 Redistributableto read and write csv, xls access etc. There is a 32 and 64 bit version of each driver.

您可以使用 Microsoft Access Database Engine 2010 Redistributable来读取和写入 csv、xls 访问等。每个驱动程序都有 32 位和 64 位版本。

回答by user334370

This is more an informational post for anyone that might be experiencing this issue (and for myself incase I have the same problem in the future and can't remember the solution :-)) It's kind of obscure but caused me a few hours of stress, so maybe it'll help someone else... Sorry if this is repeated (couldn't find it) or deprecated (some don't have the luxury of latest and greatest).

对于可能遇到此问题的任何人来说,这更像是一个信息性帖子(对于我自己,以防我将来遇到同样的问题并且不记得解决方案:-))这有点晦涩,但给我带来了几个小时的压力,所以也许它会帮助其他人......对不起,如果这是重复的(找不到)或已弃用(有些没有最新和最好的奢侈品)。

If you are using trying to use Jet 4.0 to access MS Excel documents (or other data files) on a x64-based server, you will have discovered that there is no support for this combination.

如果您尝试使用 Jet 4.0 访问基于 x64 的服务器上的 MS Excel 文档(或其他数据文件),您会发现不支持这种组合。

The only solution is to allow IIS to run 32-bit applications on Windows 64 and to install a supported db provider.

唯一的解决方案是允许 IIS 在 Windows 64 上运行 32 位应用程序并安装受支持的 db 提供程序。

You'll need to install the driver, 64-Bit OLEDB Provider for ODBC (MSDASQL) that acts as a bridge: "The Microsoft OLE DB Provider for ODBC (MSDASQL) is a technology that allows applications that are built on OLEDB and ADO (which uses OLEDB internally) to access data sources through an ODBC driver. MSDASQL is an OLEDB provider that connects to ODBC, instead of a database. MSDASQL ships with the Windows operating system, and Windows Server 2008 & Windows Vista SP1 are the first Windows releases to include a 64-bit version of the technology." Download here : http://www.microsoft.com/downloads/details.aspx?FamilyID=000364db-5e8b-44a8-b9be-ca44d18b059b&displaylang=en

您需要安装驱动程序,用于 ODBC 的 64 位 OLEDB 提供程序 (MSDASQL),它充当桥梁:“Microsoft OLE DB Provider for ODBC (MSDASQL) 是一种允许基于 OLEDB 和 ADO 构建的应用程序的技术(它在内部使用 OLEDB) 通过 ODBC 驱动程序访问数据源。MSDASQL 是连接到 ODBC 而不是数据库的 OLEDB 提供程序。MSDASQL 随 Windows 操作系统一起提供,Windows Server 2008 和 Windows Vista SP1 是第一个 Windows 版本包括该技术的 64 位版本。” 在这里下载:http: //www.microsoft.com/downloads/details.aspx?FamilyID=000364db-5e8b-44a8-b9be-ca44d18b059b& displaylang=en

This all works fine, but I came across two things that had me scratching my head (and stressing): 1) You need to allow 32-bit ASP.Net in IIS Web Service Extensions - Read ""http://www.textcontrol.com/blog/permalink/2006082101""for instructions on both enabling 32-bit apps AND the IIS web service extension setup. 2) If you are using any registry keys under IIS x64, a new node will be added in the registry - Wow6432Node - into which you'll need to move/copy any relevant keys that were used under x64. i.e. We had a data key stored in HCLM\Software\CustomKey that was no longer available when 32-bit was enabled. We re-created the key under the Wow6432Node and all was good.

这一切都很好,但我遇到了两件事让我摸不着头脑(并强调):1)您需要在 IIS Web 服务扩展中允许 32 位 ASP.Net - 阅读“” http://www.textcontrol .com/blog/permalink/2006082101""有关启用 32 位应用程序和 IIS Web 服务扩展设置的说明。2) 如果您在 IIS x64 下使用任何注册表项,则会在注册表中添加一个新节点 - Wow6432Node - 您需要将在 x64 下使用的任何相关项移动/复制到其中。即我们有一个存储在 HCLM\Software\CustomKey 中的数据密钥,当启用 32 位时该密钥不再可用。我们在 Wow6432Node 下重新创建了密钥,一切都很好。