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

6
  • looks like when of your references is missing (Microsoft.SqlServer.BatchParser)
    – Avi Turner
    Commented Aug 19, 2013 at 8:32
  • Btw, your first if is missing a closing bracket.
    – Artless
    Commented Aug 19, 2013 at 8:33
  • @AviTurner: Checked that earlier also..Now,once again I checked..But that didnt worked for me.. Commented Aug 19, 2013 at 8:35
  • @Trickery:Doesn't think so...If you were right then it should throw some syntax error, i guess..and also i couldn't find any mistake there. Commented Aug 19, 2013 at 8:37
  • @VysakhVenugopal Well, then you copied the code wrong. You have two else statements one after the other.
    – Artless
    Commented Aug 19, 2013 at 8:38

5 Answers 5

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

8
  • When the exception is thrown, can you tell what action was taking place? take a look at the stack trace. It might be happening from another referenced project.
    – Avi Turner
    Commented Aug 19, 2013 at 8:41
  • var output = server.ConnectionContext.ExecuteNonQuery(@"D:\Testpgm\master.sql"); This is where the exception occurs..What is stack trace? Commented Aug 19, 2013 at 8:43
  • call stack. its a Debug window that helps monitor the stack (hit Ctrl D, C for opening it).
    – Avi Turner
    Commented Aug 19, 2013 at 8:45
  • showing the key combination is bound to command call stack which is not currently available..I dont think it is available in VS2010 Commented Aug 19, 2013 at 8:48
  • For VS2010, it's found at Debug > Windows > Call Stack in the toolbar
    – Darkzaelus
    Commented Aug 19, 2013 at 12:29
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.

6
  • 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.. Commented Aug 19, 2013 at 8:44
  • @VysakhVenugopal I think you have to reinstall it. Please check my answer.
    – Ajay
    Commented Aug 19, 2013 at 8:47
  • @VysakhVenugopal . Looking around, this seems right. take a look at this 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
    Commented Aug 19, 2013 at 8:50
  • @VysakhVenugopal Please verify the version.
    – Ajay
    Commented Aug 19, 2013 at 8: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.. Commented Aug 19, 2013 at 8:56
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>
0
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!!");
}
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.