tvr
tvr

Reputation: 21

Run SQL Scripts from Windows Installer on MS SQL 2012

I've created a Windows Installer for my software - which works fine on all Windows versions in combination with MS SQL 2008 server. However, with the introduction of MS SQL 2012, the account that the Windows Installer is using (SYSTEM_LOCAL) has gotten its "sysadmin" privileges revoked by default, thus making it impossible for me to run the SQL scripts from the installer directly - without having the customer add the "sysadmin" role before running the installer.

The SQL scripts contains SQL commands for building the application database, setting up the CLR functions and so on.

In code behind, i use the following standard method of executing SQL from C#:

/// <summary>
/// Execute the sql script from file
/// </summary>
/// <param name="serverName"></param>
/// <param name="user"></param>
/// <param name="password"></param>
/// <param name="dbName"></param>
/// <param name="Sql"></param>
private void ExecuteSql(string serverName, string user, string password, string dbName, string Sql)   
{   
    string connStr;
    string userName = user.Trim();

    // Check if Integrated security is used
    if (userName == "")
    {
        // Use integrated security
        connStr = "Data Source=" + serverName + ";Initial Catalog=" + dbName + ";Integrated Security=True";
    }
    else
    {
        // Use username and password
        connStr = "Data Source=" + serverName + ";Initial Catalog=" + dbName + ";Integrated Security=False;User Id=" + user + ";Password=" + password;
    }

    using (SqlConnection conn = new SqlConnection(connStr))   
    {   
        try  
        {
            Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server(new ServerConnection(conn));   
            server.ConnectionContext.ExecuteNonQuery(Sql);
            Log("Succeeded");
        }   
        catch (Exception ex)   
        {   
            Log(ex.ToString());   
        }   
    }   
}

I was wondering if there is some other way of running my SQL code from the installer, using an account in MS SQL 2012 that has enough privileges to run SQL commands. I've looked at some installers that seems to be executing SQL from a command window - and most likely is running those command windows using an account that allows for SQL commands to be run on the SQL server, however I've failed at figuring out how they are doing it.

Any tips, or help would be appreciated immensly.

Im sorry for the bad english.

Upvotes: 2

Views: 1017

Answers (1)

Christopher Painter
Christopher Painter

Reputation: 55601

I would consider using Windows Installer XML (WiX) or InstallShield to author your MSI. Both have patterns / extensions to Windows Installer to facilitate executing SQL scripts against a connection without you having to reinvent the wheel.

SqlDatabase Element (Sql Extension)

SQL Scripts View

The fastest written and highest quality installers are those that focus on what needs to be done and not how to do it. The heavy lifting should be provided by the framework to increase reliability and common user experience.

Upvotes: 1

Related Questions