Execute installation script.

Hello. I will export  the database generated script for all objects, I want to make an installer that executes that script on the remote server, I think the installer must ask for sa password; anyway thats not the problem, How can I make with SMO execute an script file

Thanks




Answer this question

Execute installation script.

  • topinambour

    Use Microsoft.SqlServer.Management.Smo.Database.ExecuteNonQuery Method with your script in parameter
    (at first you must create a database)


  • Kunal Cheda

    How can I Pass this method a .sql file

  • uclimng

    If you see DotNetNuke when you navigate to the first page it takes a lot of time, that page creates all the objects on the database taking as input some .sql file that are already on the package.  However its in .net 1.1 and not 2.0 and I want to use SMO. 

    It seems to be very difficult because I havent find good info about this.

    Tks   

  • Yaniv Feinberg

    SMO expects you to either use the SMO object model to modify the database interactivly or else script the changes and run them externally later.  If you are just executing SQL script, you can use sqlcmd.exe for that. 

    For something fancier that gets passwords and other user input and then runs the script, you would need to write a wrapper script/application to gather the user input and then send it off to the server.

  • Anoop.H.TVM

    I use this:


    string script = System.IO.File.ReadAllText(file);
     


  • beso

    You can do something like:

    using System.IO;

    ~~~~~~~~~~~~~~~
    Server
    srv = new Server("MyServer");
    string filePath = "c:\\create.sql";

    FileStream
    file = new FileStream(filePath, FileMode.Open, FileAccess.Read);

    StreamReader sr = new StreamReader(file);

    string s = sr.ReadToEnd();

    sr.Close();

    srv.Databases["tempdb"].ExecuteNonQuery(s);



  • David Keaveny

    Public Shared Function RunScript(ByVal script As String, ByVal onServer As String) As Boolean
    Try
    Dim SMOServer As New Microsoft.SqlServer.Management.Smo.Server(onServer)
    SMOServer.ConnectionContext.ExecuteNonQuery(script)
    Return True
    Catch ex As Exception
    Return False
    End Try
    End Function

    Where onServer would be something like (local) or (local)\instance

    This works fine for me for the most part since the end users generaly dont have the database connection open at the time. But if they do have it open, then you first need to kill all connections to your database. Also your script needs to look something like this:

    USE [YourDBNameHere]
    GO

    YOUR SCRIPT
    GO

  • Execute installation script.