locked
Web setup deployment with Db Script RRS feed

  • Question

  • User1469676138 posted

    i am having a solution and inside,having one web setup project and a customAction Class library.Class library simply installs the database script included in the class library as embedded resource. The code in custom Action class library which installs the database is

       

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Configuration.Install;
    using System.Linq;
    using System.Reflection;
    using System.IO;
    using System.Data.SqlClient;
    using Microsoft.SqlServer.Server;
    using Microsoft.SqlServer.Management.Smo;
    using Microsoft.SqlServer.Management.Common;
    
    
    
    namespace DbCustomAction
    {
        [RunInstaller(true)]
        public partial class CustomInstaller : Installer
        {
    
            private string logFilePath = "C:\\SetupLog.txt";
            public CustomInstaller()
            {
                //This call is required by the Component Designer.
                //Add initialization code after the call to InitializeComponent
                InitializeComponent();
            }
    
            private string GetSql(string Name)
            {
                try
                {
                    Log("GetSql Code 1");
                    // Gets the current assembly.
                    Assembly Asm = Assembly.GetExecutingAssembly();
    
                    Log("GetSql Code 2");
                    // Resources are named using a fully qualified name.
    
                    Log(Asm.GetName().Name + "." + Name);
    
                    Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);
    
                    Log("GetSql Code 3");
                    // Reads the contents of the embedded file.
                    StreamReader reader = new StreamReader(strm);
    
                    return reader.ReadToEnd();
    
                    Log("GetSql Code Completed");
                }
                catch (Exception ex)
                {
                    Log(ex.ToString());
                    throw ex;
                }
            }
            private void ExecuteSql(string serverName, string dbName, string Sql)
            {
                Log("ExecuteSql 1");
                string connStr = "Data Source=" + serverName + ";Initial Catalog=" + dbName + ";Integrated Security=True";
                using (SqlConnection conn = new SqlConnection(connStr))
                {
                    try
                    {
                        Log("ExecuteSql 2");
                        Server server = new Server(new ServerConnection(conn));
                        Log("ExecuteSql 3");
                        server.ConnectionContext.ExecuteNonQuery(Sql);
                        Log("ExecuteSql 4");
                    }
                    catch (Exception ex)
                    {
                        Log("ExecuteSql Exception");
                        Log(ex.ToString());
                    }
                }
            }
            protected void AddDBTable(string serverName)
            {
                try
                {
                    //Creates the database and installs the tables.         
                    string strScript = GetSql("FreshData.txt");
                    ExecuteSql(serverName, "master", strScript);
                }
                catch (Exception ex)
                {
                    //Reports any errors and abort.
                    Log(ex.ToString());
                    throw ex;
                }
            }
    
            public override void Install(System.Collections.IDictionary stateSaver)
            {
                base.Install(stateSaver);
                Log("Setup started");
                if (!String.IsNullOrEmpty(this.Context.Parameters["servername"].ToString()))
                {
                    Log("Server Found : Name = " + this.Context.Parameters["servername"].ToString());
                    Log("Call to AddDBTable");
                    AddDBTable(this.Context.Parameters["servername"]);
                }
                else
                {
                    Log("Setup failed to start");
                }
            }
            public void Log(string str)
            {
                StreamWriter Tex;
                try
                {
                    Tex = File.AppendText(this.logFilePath);
                    Tex.WriteLine(DateTime.Now.ToString() + " " + str);
                    Tex.Close();
                }
                catch
                { }
            }
    
        }
    }
    


    when i install a setup , based on the logging,i am getting the following log.On executing db script, exception is thrown.however web site is getting installed succesfully in IIS.

    2/27/2013 3:57:22 PM Setup started 2/27/2013 3:57:22 PM Server Found : Name =[Some Server] 2/27/2013 3:57:22 PM Call to AddDBTable 2/27/2013 3:57:22 PM GetSql Code 1 2/27/2013 3:57:22 PM GetSql Code 2 2/27/2013 3:57:22 PM DbCustomAction.FreshData.txt 2/27/2013 3:57:22 PM GetSql Code 3 2/27/2013 3:57:22 PM ExecuteSql 1 2/27/2013 3:57:22 PM ExecuteSql 2 2/27/2013 3:57:22 PM ExecuteSql 3 2/27/2013 3:57:22 PM ExecuteSql Exception 2/27/2013 3:57:22 PM System.IO.FileLoadException: 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.    at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type)    at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)    at System.Reflection.Assembly.GetType(String name, Boolean throwOnError)    at Microsoft.SqlServer.Management.Common.ServerConnection.GetStatements(String query, ExecutionTypes executionType, Int32& statementsToReverse)    at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)    at DbCustomAction.CustomInstaller.ExecuteSql(String serverName, String dbName, String Sql)

    PLease help me here,why i am getting this exception and how can i resolve this issue Thanks in Advance

    Thursday, February 28, 2013 12:45 AM

Answers

  • User-166373564 posted

    Hi Hassam

    .net 4 frame work.Microsoft has not announced SMO dlls for .net frame work 4.0 .

    SMO's usage against .Net 4.0 hasn’t been signed off, but it seems that there is unsupported option to get that working(add this to app.config):

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

      <startup useLegacyV2RuntimeActivationPolicy="true">

        <supportedRuntime version="v4.0"/>

      </startup>

    </configuration>

    Moreover, I think you could give a feedback to Microsoft connect site(https://connect.microsoft.com/VisualStudio/Feedback), and check whether they have good workaround for you, Microsoft engineers will evaluate this seriously.

    hope it helps you,

    With regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 6, 2013 9:42 PM

All replies

  • User522486851 posted

    It may be a .net framework version issue, Check development framework and client machine framework version.

    Thursday, February 28, 2013 1:08 AM
  • User1196771204 posted

    hi Hassam,

    This issue is pertaining the .NET framework that is not configured properly on your system. Can you please try to deploy it to the other system? I am sure it will make differences.

    Please mark this response as an "answer" if it helps you. Thanks heaps 

    Thursday, February 28, 2013 2:16 AM
  • User1469676138 posted

    thank you so much for your comment Sanjkalpa .In my situation, client and machine framework are same as i am using my own machine environment here for testing my application.

    Tuesday, March 5, 2013 3:41 AM
  • User1469676138 posted

    very thankful to you for your reply necro. this is a known issue in .net 4 frame work.Microsoft has not announced SMO dlls for .net frame work 4.0 . i am now searching for an alternative.

    Tuesday, March 5, 2013 3:43 AM
  • User-166373564 posted

    Hi Hassam

    .net 4 frame work.Microsoft has not announced SMO dlls for .net frame work 4.0 .

    SMO's usage against .Net 4.0 hasn’t been signed off, but it seems that there is unsupported option to get that working(add this to app.config):

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

      <startup useLegacyV2RuntimeActivationPolicy="true">

        <supportedRuntime version="v4.0"/>

      </startup>

    </configuration>

    Moreover, I think you could give a feedback to Microsoft connect site(https://connect.microsoft.com/VisualStudio/Feedback), and check whether they have good workaround for you, Microsoft engineers will evaluate this seriously.

    hope it helps you,

    With regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 6, 2013 9:42 PM