简体   繁体   English

在MS SQL 2012上从Windows Installer运行SQL脚本

[英]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. 我已经为我的软件创建了Windows Installer-在与MS SQL 2008服务器结合使用的所有Windows版本上均可正常运行。 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. 但是,随着MS SQL 2012的引入,Windows Installer使用的帐户(SYSTEM_LOCAL)在默认情况下已撤销其“ sysadmin”特权,因此,我无法直接从安装程序运行SQL脚本-无需客户在运行安装程序之前添加“ sysadmin”角色。

The SQL scripts contains SQL commands for building the application database, setting up the CLR functions and so on. SQL脚本包含用于构建应用程序数据库,设置CLR函数等的SQL命令。

In code behind, i use the following standard method of executing SQL from C#: 在后面的代码中,我使用从C#执行SQL的以下标准方法:

/// <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. 我想知道是否还有其他方法可以使用具有足够特权来运行SQL命令的MS SQL 2012中的帐户从安装程序运行SQL代码。 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. 我看过一些似乎从命令窗口执行SQL的安装程序-很有可能正在使用允许SQL命令在SQL Server上运行的帐户运行这些命令窗口,但是我无法弄清楚他们是如何做到的。

Any tips, or help would be appreciated immensly. 任何提示或帮助将不胜感激。

Im sorry for the bad english. 对不起,英语不好。

I would consider using Windows Installer XML (WiX) or InstallShield to author your MSI. 我会考虑使用Windows Installer XML(WiX)或InstallShield来创作您的MSI。 Both have patterns / extensions to Windows Installer to facilitate executing SQL scripts against a connection without you having to reinvent the wheel. 两者都具有Windows Installer的模式/扩展,以方便针对连接执行SQL脚本,而无需重新发明轮子。

SqlDatabase Element (Sql Extension) SqlDatabase元素(Sql扩展)

SQL Scripts View SQL脚本视图

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. 框架应提供繁重的工作,以提高可靠性和共同的用户体验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM