【问题标题】:FileNotFoundException in c# connected with some dllc#中的FileNotFoundException与一些dll连接
【发布时间】:2013-08-19 08:30:40
【问题描述】:

这是我更新的问题..

当我尝试使用 c# 在数据库中执行一些 sql 文件时,我收到如下所示的错误:

我的 c# 代码是这样的:

if (comboBox1.SelectedItem.ToString() == "master" && comboBox2.SelectedItem.ToString() == "master.sql")
{
    oConnection.Open();
    Server server = new Server(new ServerConnection(oConnection));
    if (MessageBox.Show("Execute " + comboBox2.SelectedItem.ToString() + " in the database " + comboBox1.SelectedItem.ToString() + " ?", "Execute?", MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        var output = server.ConnectionContext.ExecuteNonQuery(@"D:\Testpgm\master.sql");
        if (!output.Equals(0))
        {
            try
            {
                MessageBox.Show(comboBox2.SelectedItem.ToString() + " executed successfully in " + comboBox1.SelectedItem.ToString() + " database");
            }
            catch (Exception exc)
            {
                MessageBox.Show("Script Execution Failed,"+exc);
            }
        }
    }
    else
    {
        MessageBox.Show("Execution cancelled by the user");
    }
    else
    {
        MessageBox.Show("Either the database or the sql script file selected is wrong!!");
    }

谁能指出为什么会发生这种情况?我已经尝试了几乎所有我在谷歌搜索时发现的东西,但不幸的是得到了我所期望的......

我还尝试将绑定重定向添加到我的 app.config,因为我可以在 c:\windows\assembly 中找到“microsoft.sqlserver.batchparser.dll”版本为 10.0.0.0,而我的 c# 应用程序查找版本 9.0 .242.0,这似乎又不起作用。我使用的代码是:

<runtime>
<assemblyBinding xmlns="urn:schemas=microsoft-com:asm.v1">
<dependentAssembly>
  <assemblyIdentity name="microsoft.sqlserver.batchparser" publicKeyToken="89845dcd8080cc91" culture="neutral"/>
  <bindingRedirect oldVersion="9.0.242.0" newVersion="10.0.0.0"/>
  <publisherPolicy apply="no"/>
</dependentAssembly>
</assemblyBinding>
</runtime>

任何帮助将不胜感激..

【问题讨论】:

  • 看起来您的参考资料丢失了 (Microsoft.SqlServer.BatchParser)
  • 顺便说一句,您的第一个 if 缺少右括号。
  • @AviTurner:之前也检查过..现在,我再次检查..但这对我没有用..
  • @Trickery: 不这么认为......如果你是对的那么它应该会抛出一些语法错误,我猜......而且我在那里找不到任何错误。
  • @VysakhVenugopal 好吧,那么您复制的代码错误。您有两个 else 语句一个接一个。

标签: c# sql


【解决方案1】:

看起来您的引用何时丢失 ('Microsoft.SqlServer.BatchParser') 。 为了验证这一点,请执行以下操作:

  1. 打开您的解决方案资源管理器 (Ctrl+w, s)。
  2. 扩展您的项目。
  3. 展开引用“文件夹”
  4. 找到“Microsoft.SqlServer.BatchParser”引用。

“Microsoft.SqlServer.BatchParser”引用是否以感叹号突出显示?这么想...
突出显示它并按 F4 获取属性。

在属性中找到路径。我怀疑它是空白的,或者文件真的不存在(正如例外所暗示的那样......)

【讨论】:

  • 当异常被抛出时,你能知道发生了什么动作吗?看看堆栈跟踪。它可能发生在另一个引用的项目中。
  • var output = server.ConnectionContext.ExecuteNonQuery(@"D:\Testpgm\master.sql");这是发生异常的地方..什么是堆栈跟踪?
  • 调用堆栈。它是一个有助于监控堆栈的调试窗口(按 Ctrl D、C 打开它)。
  • 显示组合键绑定到当前不可用的命令调用堆栈..我认为它在 VS2010 中不可用
  • 对于 VS2010,它位于工具栏中的 Debug &gt; Windows &gt; Call Stack
【解决方案2】:

我能够使用 10.* 版本的程序集“Microsoft.SqlServer.ConnectionInfo”、“Microsoft.SqlServer.Management.Sdk.Sfc”和“Microsoft.SqlServer.Smo”成功运行您的代码。尝试下载 SMO 组件的 2008 版本,也许这是他们现在修复的错误。

【讨论】:

  • MS SQL Server 2008 管理对象和 Microsoft SQL Server 管理对象集合是否不同?我已经安装了 MS SQL Server 2008 管理对象..
  • @VysakhVenugopal 我认为您必须重新安装它。请检查我的答案。
  • @VysakhVenugopal。环顾四周,这似乎是正确的。看看this SO 线程。这解释了为什么它不能作为参考(它安装在 GAC 中)。我怀疑如果你已经安装了它,可能会有版本差异。
  • @VysakhVenugopal 请验证版本。
  • @AviTurner:是的..程序集中那个 dll 的版本号是 10.0.0.0。但我的 c# 应用程序查找版本 9.0.242.0。但是在我的 app.config 中,我添加了一个将其重定向到新版本的条目,这似乎也不起作用..
【解决方案3】:

.Net 运行时的不同版本?如果您的应用程序使用 .Net4,但要加载的 dll 是使用 .Net2 创建的,您可以在配置部分添加一个条目:

<startup useLegacyV2RuntimeActivationPolicy="true"></startup>

【讨论】:

    【解决方案4】:

    Atlast 我已经找到了解决方案。只是对我的代码进行了一些更改,如下所示:

    if (comboBox1.SelectedItem.ToString() == "master" && comboBox2.SelectedItem.ToString() == "master.sql")
    {
    oConnection.Open();
    ***SqlCommand SqlCmd = new SqlCommand();
    SqlCmd.CommandText = textentry;
    SqlCmd.Connection = oConnection;
    var output = SqlCmd.ExecuteNonQuery();***
    if (MessageBox.Show("Execute " + comboBox2.SelectedItem.ToString() + " in the database " + comboBox1.SelectedItem.ToString() + " ?", "Execute?", MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
     if (!output.Equals(0))
        {
            try
            {
                MessageBox.Show(comboBox2.SelectedItem.ToString() + " executed successfully in " + comboBox1.SelectedItem.ToString() + " database");
            }
            catch (Exception exc)
            {
                MessageBox.Show("Script Execution Failed,"+exc);
            }
        }
    }
    else
    {
        MessageBox.Show("Execution cancelled by the user");
    }
    else
    {
        MessageBox.Show("Either the database or the sql script file selected is wrong!!");
    }
    

    【讨论】:

      【解决方案5】:

      对于寻找此 dll 且无法通过相关 MS SQL Server 功能包安装的其他人,该 dll 位于:

      C:\Windows\assembly\GAC_64\Microsoft.SqlServer.BatchParser\

      然后在 SQL Server 版本的子文件夹中。如果您以 32 位运行该进程,则它将位于 GAC_32 中。将文件复制到应用程序的 bin 文件夹中,一切正常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-25
        • 2015-12-22
        • 1970-01-01
        相关资源
        最近更新 更多