Click here to Skip to main content
15,902,636 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got this error while executing the database Query
'could not load file or assembly 'microsoft.sqlserver.batchparserclient version=12.0.0.0'
Here I am using sql server 2008 R2 I installed all the smo objects.

Here is the code

C#
try
           {
               string sqlConnectionString = cn.ConnectionString.ToString();
               SqlConnection conn = new SqlConnection(sqlConnectionString);
               StringBuilder sb = new StringBuilder();
               string script = string.Empty;
               Microsoft.SqlServer.Management.Smo.Server server = new Server(new ServerConnection(conn));
               string mappath = Path.Combine(Application.StartupPath.ToString(), "Scripts");
               var files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(f => int.Parse(Path.GetFileNameWithoutExtension(f)));
               //string[] filePaths = Directory.GetFiles(@"" + mappath + "", "*.sql");
               try
               {
                   foreach (string pa in files)
                   {
                       script = File.ReadAllText(pa);
                       int counter = 0;
                       string line;
                       if (pa == mappath + @"\4.sql" || pa == mappath + @"\7.sql" || pa == mappath + @"\8.sql" || pa == mappath + @"\10.sql" || pa == mappath + @"\13.sql" || pa == mappath + @"\14.sql" || pa == mappath + @"\16.sql" || pa == mappath + @"\100.sql")
                       {
                           // Read the file and display it line by line.
                           System.IO.StreamReader file =
                              new System.IO.StreamReader(pa);
                           while ((line = file.ReadLine()) != null)
                           {
                               cmd = new SqlCommand(line);
                               cmd.Connection = conn;
                               cmd.CommandTimeout = 1000;
                               try
                               {
                                   cmd.ExecuteNonQuery();
                               }
                               catch (Exception Ex)
                               {
                                   sb.Append(line);
                                   sb.AppendLine();
                               }
                               counter++;
                           }
                           file.Close();
                       }
                       else if (pa == mappath + @"\02.sql")
                       {
                       }
                       else
                       {
                           try
                           {
                               //cmd = new SqlCommand(script);
                               //cmd.Connection = conn;
                               //cmd.CommandTimeout = 1000;
                               //cmd.ExecuteNonQuery();
                               server.ConnectionContext.ExecuteNonQuery(script); //<big>Getting error Here</big>
                           }
                           catch (Exception EX) { IsFailed = true; }
                       }

                   }
                   //conn.ChangeDatabase("master");
                   conn.Close();
               }
               catch (Exception Ex) { }


What I have tried:

I installed smo objects from this link https://www.microsoft.com/en-us/download/details.aspx?id=26728
Posted
Updated 8-Mar-16 22:22pm
v2
Comments
OriginalGriff 9-Mar-16 3:54am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So without seeing the code that generates the error there isn't a whole lot we can do to help...
Use the "Improve question" widget to edit your question and provide better information.
TarunKumarSusarapu 9-Mar-16 4:03am    
I modified the question and i indicated error at the execute nonquery method

1 solution

You need to start by looking at the content of your files: since you load the script from each file in turn and execute it, you need to use the debugger to find out exactly what is in the script it fails on.
C#
                       script = File.ReadAllText(pa);
...
                       server.ConnectionContext.ExecuteNonQuery(script);
 
Share this answer
 
Comments
TarunKumarSusarapu 9-Mar-16 4:28am    
In 32 bit system it is working fine in 64 bit i got this type of problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900