最近有个项目需要生成一个自动打包安装App和数据库的MSI文件,经同事推荐WIX,于是乎就试了一试。遇到了一些问题觉得有分享的价值,所以写篇博客记录一下 :)
使用感觉:
WIX特点:功能很强大,用XML配置实现,没有界面,弹性很大,但learning curve比较长。
WIX貌似对LocalDB不支持,如果是其它版本的SQL Server,WIX有内置配置支持,很简单。
问题一:
WIX内置不支持SQL LocalDB,怎么实现对SQL LocalDB安装及配置
解决方案:
我的第一想法和现在的做法是用Powershell调用SQL Scripts,然后再用WIX调用Powershell
代码实现:
Powershell调用SQL Scripts
$dbName = ‘LocalDBTest‘ $serverName = ‘(localdb)\localdbtest‘
# determine path of script. all paths in script are relative to script file.
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
‘ setting up database...‘
sqlcmd -U ‘sa‘ -P ‘test!123‘ -S $serverName -i "$scriptDir\.\LocalDBTestSchema.sql" -X
WIX 调用Powershell
<ComponentGroup Id="LocalDBTest_Project" Directory="LocalDBTest">
<Component Id="SetupDatabase" Guid="8DE013D2-BB7D-4958-973A-3C54606D5CDC" KeyPath="yes">
<File Id="SetupDatabase" Name="SetupDatabase.ps1" Source="$(var.ProjectDir)DB\SetupDatabase.ps1" Vital="yes" />
</Component>
...
</ComponentGroup>
<!--Define the CustomAction for running the PowerShell script-->
<CustomAction Id="RunPowerShellScript" BinaryKey="WixCA" DllEntry="CAQuietExec" Execute="deferred" Return="check" Impersonate="yes" />
<InstallExecuteSequence>
<!--Invoke PowerShell script -->
<Custom Action="RunPowerShellScript" After="InstallFiles"><![CDATA[NOT Installed]]></Custom>
</InstallExecuteSequence>
<!-- Ensure PowerShell is installed and obtain the PowerShell executable location -->
<Property Id="POWERSHELLEXE">
<RegistrySearch Id="POWERSHELLEXE"
Type="raw"
Root="HKLM"
Key="SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell"
Name="Path" />
</Property>
<Condition Message="This application requires Windows PowerShell.">
<![CDATA[Installed OR POWERSHELLEXE]]>
</Condition>
<!-- Define the PowerShell command invocation -->
<SetProperty Id="RunPowerShellScript"
Before ="InstallFiles"
Sequence="execute"
Value =""[POWERSHELLEXE]" -Version 2.0 -NoProfile -NonInteractive -InputFormat None -ExecutionPolicy Bypass -Command "& ‘[#SetupDatabase]‘ ; exit $$($Error.Count)"" />
问题二:
Window 7 对权限管控比较严,SQL Scripts对C:盘大多数目录没有创建文件的权限
解决方案:
查找了一下,发现%UserProfile%目录好像SQL有读写权限,因为我们程序首先需要安装LocalDB所以选择了以下目录
%UserProfile%\AppData\Local\Microsoft\Microsoft SQL Server Local DB
问题三:
怎么把一个%UserProfile%目录从Powershell中传给SQL Script使用
解决方案:
在Powershell中拿到%UserProfile%目录,然后调用Sqlcmd传给SQL Scripts
代码实现:
在Powershell中拿到%UserProfile%目录
$dbName = ‘LocalTestDB‘
$serverName = ‘(localdb)\localDBTest‘
# get the user profile folder which no need access rights to create DB files
$userProfile = $env:USERPROFILE
$userProfile = $userProfile+‘\AppData\Local\Microsoft\Microsoft SQL Server Local DB‘
# 注意底下双引号和单引号的使用依次是 ,开始[单引][双引][单引],结束[双引][单引],这里折腾了我不少时间,地址老是传得不对
$RingDB = ‘"‘+$userProfile+‘\LocalTestDB.mdf"‘
$RingLog = ‘"‘+$userProfile+‘\LocalTestDB_log.ldf"‘
# determine path of script. all paths in script are relative to script file.
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
调用Sqlcmd传给SQL Scripts
‘ setting up database...‘
#sqlcmd -U ‘sa‘ -P ‘test!123‘ -S $serverName -i "$scriptDir\.\LocalDBTestSchema.sql" -v varMDF= $RingDB varLDF= $RingLog
LocalDBTestSchema.sql 中对应的变量写法如下:
USE [master]
GO
/****** Object: Database [LocalTestDB] Script Date: 2014/4/1 12:24:09 ******/
CREATE DATABASE [LocalTestDB]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N‘LocalTestDB‘, FILENAME = N‘$(varMDF)‘ , SIZE = 15552KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N‘LocalTestDB_log‘, FILENAME = N‘$(varLDF)‘ , SIZE = 76736KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
问题四:
LocalDB默认安装时没有界面的,用户不会用SQL Command去创建SQL instance和修改sa的密码,App怎么用sa连接数据库呢?
解决方案:
在Powershell中用Windows 集成登陆模式启用sa账号,然后修改sa密码
代码实现:
‘ Create instance for localDB‘
SQLLocalDB create "LocalDBTest"
‘ Grant & Enable sa account‘
sqlcmd -S $serverName -E -Q "GRANT CONNECT SQL TO [sa]"
sqlcmd -S $serverName -E -Q "ALTER LOGIN [sa] ENABLE"
‘ Change sa password‘
sqlcmd -S $serverName -E -Q "EXEC sp_password NULL, ‘test!123‘,‘sa‘"
以上是本人实践的一些方法和遇到的问题及解决方案,可能不是最佳解决方案,但确实可运行\可工作的方案。
如有问题或建议,希望能和大家探讨,同时也希望本文能帮到一部分人~~