0

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

enter image description here

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

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

Vysakh Venugopal
  • 71
  • 1
  • 4
  • 10

5 Answers5

0

looks like when of your references is missing ('Microsoft.SqlServer.BatchParser') . In order to validate this do as follows:

  1. Open your solution explorer (Ctrl+w, s).
  2. Expand your project.
  3. Expand the references "folder"
  4. locate the 'Microsoft.SqlServer.BatchParser' reference.

Is the 'Microsoft.SqlServer.BatchParser' reference highlighted with an exclamation point? thought so...
Highlight it and hit F4 for properties.

In the properties locate the path. I suspect it is either blank or the file is really not there (as the exception implies...)

enter image description here

Avi Turner
  • 10,234
  • 7
  • 48
  • 75
0

I was able to successfully run your code using the 10.* versions of the assemblies "Microsoft.SqlServer.ConnectionInfo", "Microsoft.SqlServer.Management.Sdk.Sfc" and "Microsoft.SqlServer.Smo". Try downloading the 2008 version of the SMO components, maybe it was a bug that they've now fixed.

Ajay
  • 6,418
  • 18
  • 79
  • 130
  • Is MS SQL Server 2008 Management Objects and Microsoft SQL Server Management Objects Collection different? I have already installed MS SQL Server 2008 Management Objects.. – Vysakh Venugopal Aug 19 '13 at 08:44
  • @VysakhVenugopal I think you have to reinstall it. Please check my answer. – Ajay Aug 19 '13 at 08:47
  • @VysakhVenugopal . Looking around, this seems right. take a look at [this](http://stackoverflow.com/questions/3981027/cant-find-microsoft-sqlserver-batchparser-dll) SO thread. this explains why it is not available as a reference (it is installed in the GAC). I suspect that if you already installed it, there might be a version difference. – Avi Turner Aug 19 '13 at 08:50
  • @VysakhVenugopal Please verify the version. – Ajay Aug 19 '13 at 08:53
  • @AviTurner: Yes..The version number of that dll in the assembly is 10.0.0.0. But my c# application looks for the version 9.0.242.0. But in my app.config I have added an entry to redirect it to new version,that also doesn't seems to be working.. – Vysakh Venugopal Aug 19 '13 at 08:56
  • @Ajay: Sorry..That also doesn't work for me..I am not getting why this happens eventhough I have given binding redirect in my app.config. – Vysakh Venugopal Aug 20 '13 at 05:04
0

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>
Bernhard Hiller
  • 2,163
  • 2
  • 18
  • 33
0

Atlast I have found out a solution for that. Just made some change in my code like this:

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!!");
}
Vysakh Venugopal
  • 71
  • 1
  • 4
  • 10
0

For others looking for this dll and can't install it via the relevant MS SQL Server Feature Pack, the dll is located at:

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

and then in a subfolder for the version of SQL Server. If you're running the process as 32-bit, it'll be in GAC_32 instead. Copy the file into your application's bin folder and things should work.

nicodemus13
  • 2,258
  • 2
  • 19
  • 31