【问题标题】:How to fix the compiler error CS0009 - An attempt was made to load a program with an incorrect format?如何修复编译器错误 CS0009 - 尝试加载格式不正确的程序?
【发布时间】:2019-12-05 16:28:52
【问题描述】:

我正在尝试使用 csc.exe C# 编译器 reference Microsoft ActiveX 数据对象 6.1 库 (ADODB),但出现错误:

致命error CS0009:元数据文件“c:\Program Files\Common Files\system\ado\msado15.dll”无法打开——“试图加载格式不正确的程序。 '

我已简化文件以尝试跟踪问题。

批处理文件Compile.bat:

@ECHO OFF
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\csc.exe -nologo -r:"C:\Program Files\Common Files\system\ado\msado15.dll" Code.cs
PAUSE >NUL

C#文件Code.cs:

class Program {
  static void Main() {
    ADODB.Connection ADODBConnection = new ADODB.Connection();
    System.Console.WriteLine((ADODBConnection == null).ToString());
  }
}

我已经尝试copying the dll to the same folder 并引用它,但得到了同样的错误。

我还有编译器版本...\v2.0.50727\csc.exe...\v3.5\csc.exe 以及库版本msado20.tlbmsado21.tlbmsado25.tlbmsado26.tlbmsado27.tlbmsado28.tlbmsado60.tlb

当我在 Visual Studio 2017(.NET Framework 4 控制台应用程序)上尝试相同的操作时,它可以工作。它创建ConsoleApp1\ConsoleApp1\obj\Debug\Interop.ADODB.dll 并引用它,但我不知道它是从哪里得到的。我已经尝试在不同的文件夹中搜索它。

【问题讨论】:

  • 为什么哦,为什么你使用 ADODB 和 c# 而不是 ADO.Net?
  • @ChrisDunaway 因为我不知道更好。我会尝试使用它。

标签: c# batch-file dll adodb csc


【解决方案1】:

我从this thread(通过在线搜索Interop.ADODB.dll file location)和后来的this link找到了Visual Studio使用的文件:

C:\Program Files\Microsoft.NET\Primary Interop Assemblies\adodb.dll

工作Compile.bat:

@ECHO OFF
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\csc.exe -nologo -r:"%ProgramFiles%\Microsoft.NET\Primary Interop Assemblies\adodb.dll" Code.cs
PAUSE >NUL

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 2010-09-07
    • 2014-01-11
    • 1970-01-01
    • 2020-01-04
    相关资源
    最近更新 更多