【问题标题】:Cannot add a reference to c# code无法添加对 c# 代码的引用
【发布时间】:2013-11-12 23:30:26
【问题描述】:

我的设置 - .NET 3.5 和 Visual Studio 2008。 我有 c# 代码(在 ssis 中,但不用担心 ssis)。我希望能够在我的代码中使用 ADODB。因此,我添加了对 ADODB 的引用。我从 COM 选项卡中选择了 Microsoft ActiveX Data Objects 2.8 Library。然后,我添加了使用 ADODB 作为我的导入。

示例步骤在这里 -

http://www.c-sharpcorner.com/uploadfile/ptailor/adodbservice12062005002501am/adodbservice.aspx

但是,在运行项目后,我得到一个错误,我还看到引用列表中缺少引用。我发誓它在我添加后就在那里。我该如何解决这个问题?

错误-

System.IO.FileNotFoundException: Could not load file or assembly 'Interop.ADODB, Version=2.8.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

File name: 'Interop.ADODB, Version=2.8.0.0, Culture=neutral, PublicKeyToken=null'
   at ST_39348fjejencuw92092828303554.csproj.ScriptMain.Main()

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

需要这个问题-

我试图使用下面的代码来获取 ADO 记录集中的行数。当我尝试使用代码时,我不断收到错误“找不到类型或命名空间名称'ADODB'”。

ADODB.Recordset result = (ADODB.Recordset)Dts.Variables["RESULT_SET"].Value;
int rowCount = result.RecordCount;
MessageBox.Show("rowCount = " + rowCount);

【问题讨论】:

  • 答案在微软 SSIS 论坛上。同样的帖子。

标签: c# .net .net-3.5


【解决方案1】:

答案在微软 SSIS 论坛上。那里同样的帖子。添加参考后,只需使用 ctrl+shift+s 保存所有内容,或者只需从文件 > 全部保存即可。如果您不这样做,则不会保存引用。愚蠢的。 Ctrl+s 只保存代码,没有别的。我想知道为什么。


在找到答案之前我尝试了一些事情

基于我的问题得到的 cmets - 如果您想将 Visual Studio 2008 for SSIS 的“目标平台”更改为 x86,即 BIDS,然后转到构建 -> 配置管理器 -> 您可以在那里看到一些平台选项。如果你愿意,试着摆弄它们。

但是,如此处所述,更改您的平台会产生后果 - http://social.msdn.microsoft.com/Forums/sqlserver/en-US/ff154ba9-0793-4c19-8896-4fbdc119a6a3/could-not-load-file-or-assembly?forum=sqlintegrationservices#6916a8e9-8ae5-46c3-8cd2-9150dbc14bc6

如果我执行包,是否有可能遇到任何问题 32位模式?有什么缺点吗?

--

唯一的主要缺点是您的包不能使用超过 2GB 用于处理的内存。如果你的包有低内存需求,那么 在32位模式下执行绝对没问题。

【讨论】:

    【解决方案2】:

    在从 Visual Studio 运行项目时,使用以下步骤将平台目标强制为 32 位:

    ->Right click on Project
    ->Select Properties
    ->Goto Build Options
    ->Change  "Platform Target" from "ANY CPU" to "X86"
    

    现在运行项目。

    【讨论】:

    • 我可以试试这个,但我想知道这个设置是否会在将来导致任何问题。我不知道,也许如果我这样做,我的代码不会在 X64 系统上运行?对/错???
    • @blasto :它适用于 32 位和 64 位系统。
    • 谢谢。我会试试这个。你能告诉我为什么会发生错误吗?
    • 构建选项卡中没有“平台目标”。现在呢?
    • 我认为您正在查看“平台”查看“平台目标”
    【解决方案3】:

    正如异常所暗示的,FileNotFoundException 发生在以下情况:

    尝试访问磁盘上不存在的文件失败时引发的异常。

    当您引用 COM 对象时,还会创建 Interop.ADODB。转到项目的 bin\debug 文件夹,看看是否可以找到 Interop.ADDODB 文件。 如果找不到,则应检查所引用的 *.dll 文件的完整性。

    【讨论】:

    • 我进入了我的 SSIS 项目文件夹。有一个 bin 文件夹,但没有 debug 文件夹。现在,我很困惑。
    • 你构建了这个项目吗?
    • 好的,所以我按照链接中的步骤操作并成功创建了项目(构建它并且没有任何错误/警告)。从我在链接中看到的是,您应该引用 Microsoft ActiveX 数据对象 2.7 而不是您所说的引用的 2.8。尝试引用 Microsoft ActiveX 数据对象 2.7。
    • 发生了最奇怪的事情。我加了2.7。然后,我只是运行我的代码并得到了我想要的。当我回到代码时,我仍然收到错误 - 找不到类型或命名空间名称“ADODB”。我必须再次添加引用才能运行代码。真奇怪。错误也许???
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    相关资源
    最近更新 更多