0

I have this class that I am trying to execute my script from:

using System;
using System.Data.SqlClient;
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using SSS.ServicesConfig.MiscClasses;

namespace SSS.ServicesConfig.sqlchanges
{
  internal static class ExecuteScript
  {
    public static Status Execute(SqlConnection con, string scripttoexecute)
    {
      var status = new Status();
      try
      {
        var file = new FileInfo(scripttoexecute);
        var script = file.OpenText().ReadToEnd();
        var server = new Server(new ServerConnection(con));
        server.ConnectionContext.ExecuteNonQuery(script);
        file.OpenText().Close();
        status.IsSuccess = true;
        status.Message = "Success!";
        return status;
      }
      catch (Exception ex)
      {
        status.Message = ex.Message;
        status.IsSuccess = false;
        return status;
      }
    }
  }
}

It is executing a sql script, created by SSMS, that I have in the project set to copy every time.

I am getting this message:

Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

I checked every one of my projects and they are using .Net Framework 4. I get this message on this line:

server.ConnectionContext.ExecuteNonQuery(script);

Is it saying my .sql file isn't a .Net 4.0 file? I'm not sure I understand what this is referring too that is running 2.0.

Any ideas?

EDIT#1

This is the beginning of my .sql file if that helps. I can post the whole thing but it's real basic, just create a handful of tables:

USE [master]
GO
/****** Object:  Database [SuburbanPps]    Script Date: 4/7/2014 2:00:12 PM ******/
IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = N'SuburbanPps')
BEGIN
CREATE DATABASE [SuburbanPps] ON  PRIMARY 
( NAME = N'SuburbanPps', SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'SuburbanPps_log', SIZE = 2048KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
END

GO
ALTER DATABASE [SuburbanPps] SET COMPATIBILITY_LEVEL = 100
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [SuburbanPps].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [SuburbanPps] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [SuburbanPps] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [SuburbanPps] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [SuburbanPps] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [SuburbanPps] SET ARITHABORT OFF 
GO

EDIT#2

I believe it is the Microsoft.SqlServer.ConnectionInfo since it's version is v2.0.50727, which matches the above version.

EDIT#3

Ok, I'm confused on this. I would have thought, when I added Microsoft.SqlServer.ConnectionInfo that I was adding the .Net 4.0 version as shown from the picture below:

enter image description here

Is that not correct? I'd rather not have to require two different frameworks when installing my application if I do not need to. Or... am I really confused on what is going on?

Isn't this just a work around instead of a real fix for the issue:

<startup useLegacyV2RuntimeActivationPolicy="true">
ErocM
  • 4,505
  • 24
  • 94
  • 161
  • It doesn't seem related to your sql script at all. – Crono Apr 07 '14 at 19:43
  • I wouldn't think it would... I just don't understand what it is referring to. – ErocM Apr 07 '14 at 19:44
  • This looks like one of your components is not running the same framework as the rest, perhaps your sql server driver is not up to date. – crthompson Apr 07 '14 at 19:45
  • Have you tried this out: http://stackoverflow.com/questions/2455654/what-additional-configuration-is-necessary-to-reference-a-net-2-0-mixed-mode – PCG Apr 07 '14 at 19:46
  • Likely you have multiple projects in your solution and they're targetting different .NET runtime versions. That, or you are referring to a compiled assembly that is. – Crono Apr 07 '14 at 19:46
  • http://social.msdn.microsoft.com/Forums/en-US/1f60a379-e2e3-46b9-b343-0235486fc746/mixed-mode-assembly-is-built-against-version-v2050727-of-the-runtime-and-cannot-be-loaded-in-the?forum=clr – crthompson Apr 07 '14 at 19:48
  • @P_G That looks like it. Any way of bringing the ConnectionInfo up to date? – ErocM Apr 07 '14 at 19:48
  • What's the complete exception stack trace? Does the error occurs on the client side? Is there any SQLCLR assemblies implied? – Crono Apr 07 '14 at 19:48
  • @ErocM How could that look like it? You said you didn't have any non 4.0 library references? – Crono Apr 07 '14 at 19:50
  • @Crono I didn't notice you said this part Crono `you are referring to a compiled assembly that is` which seems to be the case – ErocM Apr 07 '14 at 19:52
  • @ErocM Ah, I see. :) Then yes that's the problem. – Crono Apr 07 '14 at 19:53
  • I have added EDIT#3. I think I've confused myself even further... – ErocM Apr 07 '14 at 19:59

1 Answers1

2

Mixed Mode Assembly? Is this an ASP.Net application?

If so this has been answered and resolved several times. A small change to the Web.config should work.

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0"/>
</startup>

Ref

Community
  • 1
  • 1
Perkentha
  • 27
  • 3
  • I've edited my post and added Edit#3 in response to this. It's a winforms application. – ErocM Apr 07 '14 at 20:02
  • I've had this problem with console apps in the past, and this is the same fixed I made there. – Martin Costello Apr 07 '14 at 20:17
  • This is good to know, most of my work is in the proverbial "cloud" so its all ASP on the .NET side of the house. The overall solution would be to bring all referenced libraries up to v4.0. Microsoft.SqlServer.ConnectionInfo you may want to copy this resource local to the project if you are having issues with it recognizing the 4.0 version as opposed to the 2.0 version. I have had wonky issues in the past with Visual Studio handling the different versions of the same resource. – Perkentha Apr 07 '14 at 20:29
  • `The overall solution would be to bring all referenced libraries up to v4.0.` That's exactly what I am trying to find out. I will try to copy it the project and see how that works out. – ErocM Apr 07 '14 at 21:59