c#中的FileNotFoundException与某些dll连接 [英] FileNotFoundException in c# connected with some dll

查看:158
本文介绍了c#中的FileNotFoundException与某些dll连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我更新的问题..



当我尝试使用c#在数据库中执行某些sql文件时,我收到错误,如我的问题所示这里:http://stackoverflow.com/questions/18309459/filenotfoundexception-in-c-sharp-connected-with-some-dll



我的c#代码就像:



This is my updated question..

When I try to execute some sql file in a database using c#, I am getting the error as shown in my question here: http://stackoverflow.com/questions/18309459/filenotfoundexception-in-c-sharp-connected-with-some-dll

My c# code is like:

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!!");
    }





有人能指出为什么会这样吗?我已经尝试了几乎所有我在google搜索时找到的东西,但不幸的是得到了我的预期..



我也尝试过将绑定重定向添加到我的app.config因为我可以在c:\windows \ assembly中找到microsoft.sqlserver.batchparser.dll版本为10.0.0.0,而我的c#应用程序会查找版本9.0.242.0,这似乎无法正常工作。我使用的代码是:





Can anyone point out why this happens? I have tried almost all the things I found while googling, but unlucky to get what I expected..

I have also tried like adding binding redirect to my app.config since I could find "microsoft.sqlserver.batchparser.dll" version as 10.0.0.0 inside c:\windows\assembly and my c# application looks for the version 9.0.242.0, which again doesnt seems to be working. The code i used is:

<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>





任何帮助都会非常感激..



Any help would be really appreciated..

推荐答案

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

Different versions of the .Net runtime? If your application uses .Net4, but the dll to be loaded was created with .Net2, you can add an entry into the configuration section:
<startup useLegacyV2RuntimeActivationPolicy="true"></startup>


Atlast我找到了解决方案。刚刚对我的代码进行了一些更改,以便使用这样的ADO.Net组件:(标记为***)



Atlast I have found out a solution for that. Just made some change in my code to use ADO.Net components like this:(marked ***)

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!!");
}


这篇关于c#中的FileNotFoundException与某些dll连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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