How to create a MSI for a database by NANT script - database

I have some batch files which will call sql scripts internally.These sql scripts will create the database.Here what is my task is to create a msi which will accept username,password,etc and if user credientials are correct then that user interface should allow user to run that batch fiels.

This seems like a very customized installer, so you need a setup authoring tool. If you want something free you can try WiX.

Related

Definitive/Guaranteed Approach to Execute Batch Script Post MSI Installation using Visual Studio 2013

I've been cobbling some post installation steps but having difficulties, which I will go into. Please recommend a suitable, ideally native, process to run some custom steps post installation.
Using:
Visual Studio 2013 Ultimate
Setup:
vb.NET project & Visual Studio Instillation Project
Test OS:
Win 7 x64 with Admin
Currently, installer successfully install main application and extracts MS SQL Express + SQL scripts to a subdirectory. I have a batch file ("InstallSQL.bat") in the same directory, which silently installs SQL Express, and then executes the SQL scripts.
So how to best execute the "InstallSQL.bat" script, when Visual Studio doesn't support batch execution, from an installer Custom Action?
Methods I've tried:
Add cmd.exe (32-bit & 64-bit) + Installer Custom Action to launch the script, as per this post. For some reason, the cmd.exe is executed with non-administrator credential, and SQL Setup fails.
Use a VBS script, to launch the batch script. VBS script does not run and error "A script required for this install to complete could not be run".
I am happy to consider an alternative approach to install SQL Express and run scripts, not based on a batch file. However, my custom batch file works perfectly when run manually i.e. not from the installer.
Please note, it needs to work on Windows XP and up, be location insensitive i.e. no static file locations and ideally, without using 3rd party utilities. This last requirement is weak.
I do that with a config file
"YourSQLExeFile" /CONFIGURATIONFILE="FullPath\YourConfigFile"
You did not specify what version of SQL you are using (2008/2012/2014, etc). You can find examples with a google search... otherwise I may be able to share what I use if you reply with SQL version.
This is all so error prone your best bet is to do it as part of the configuration of the app when it first runs.
Any custom action code that runs with user credentials (typically because it is a "just me" install) will not run elevated.
Any custom action code that runs in a per machine Everyone install will run elevated with the system account, which typically is not helpful because the system account doesn't have database credentials or network credentials (if the database is on a network share it doesn't work).
If your vbscript fails you'll need to do the install with a verbose log to get diagnostics. A common error is to use the WScript object, but that's available only if you initiate the script with WSH, and Windows Installer is not WSH.
This is why it's better to do it after the install, initiated by the user rather than by an msiexec process running with the system account or impersonating the installing user.
You best hope of doing this from the install is for you to write code to read that script and configure the database. The SQLCommand class, for example, as here:
Sql Scripot with C#
Run SQL script
Run a SQL Script
This executable needs a manifest requesting administrator privilege so when it runs it prompts for elevation. You fire off this program from your VBScript custom action. The point is that this elevation and the fact that it's shell-executed as a separate process gives some independence from the msiexec.exe service process that is calling your custom actions. Again, Everyone vs. Just me installs will make a difference, and you may find that your executable ends up running with the system account. The good news is that if this doesn't work from the install you just need to arrange for it to run the first time someone runs your app, when it will in a normal interactive user environment, and of course you can test and debug this in the normal way.

Customise SQL Compare script output for Database Project

We've recently started using Database Projects in Visual Studio 2013. So far, it's great!
There are some things we'd like to be able to customise and I'm not sure if it's even possible.
We'd like to be able to have our post-deployment scripts appended to the SQL that is generated when doing a schema compare, so that when we create a deployment script, it comes with our post-deployment stuff all in the one .sql file. Is there a way to do that?
Also, we'd like to know if there's a way that the .sql file that is generated can be customised (much like a .tt file can be for entity framework).
Welcome to SSDT!
Pre/Post deploy scripts don't get used by the schema compare so if you generate a lot of scripts then set up PowerShell or a batch file to call sqlpackage.exe and set the action to script. I use this as part of a CI build to generate scripts for different environments and works great.
You can customize the output by choosing what to include or exclude in the output script but if there is something not supplied out of the box you can modify it by using deployment contributors which you can add or remove steps or do things like add your own transactions etc, for more details see:
http://blogs.msdn.com/b/ssdt/archive/2013/12/23/dacfx-public-model-tutorial.aspx
If you let us know more about how you want to change it, there may be something already available to help.
Ed

How to configure Visual Studio 2012 Database Project to Build deployment .sql file

Does anyone know How to configure Visual Studio 2012 Database Project to Build a deployment .sql file package.
I'm attempting to not have to use the .dacpac as my only deployment option.
e.g. generate a .sql file of all database schema alters.
If you're trying to do this automatically, you'll want to use the SQLPackage command to generate a script. If you want to do this within the IDE, publish the database and choose the option to generate a script instead of publishing the changes.
I'll usually build the project first with msbuild. That will generate a dacpac against which I can run the SQLPackage command.
My batch file looks something like this:
msbuild .\MyDB\MyDB.sqlproj /t:build /p:Configuration="Local"
sqlpackage /a:DeployReport /tsn:FTPROD /sf:.\MyDB\sql\Local\MyDB.dacpac /pr:.\MyDB\Publish\Production.publish.xml /op:.\Release\MyDB-Production.xml
sqlpackage /a:script /tsn:DBServer /sf:.\MyDB\sql\Local\MyDB.dacpac /pr:.\MyDB\Publish\Production.publish.xml /op:.\Release\MyDB-Production.sql
The first line builds the project. The second creates a deploy report so I can easily see what's going to be changed. The last generates the script. Your paths may vary so you'll need to tweak as appropriate for your environment. You'll need to be able to access the database against which the script will be run in order to generate the script.

wix include files in setup but dont copy to TARGETDIR

I am writing a wix installer and I have used it to create a sql database.
We have a custom command line tool that we need to run to populate the database with its schema and data, the tool requires sql scripts to be included, we have quite a few and they are organised into various directories.
Is it possible to include these files in the installer and then run the command line tool against these files without having to copy these files to the install target dir.
I'm thinking perhaps I will have to copy these files to temp and then after install is finished clean them up, is there a better way?
You can create an msi custom action and embed sql scripts as resources within the dll itself.
Then schedule your custom action to run the exe on those sql scripts.
The msi engine will automatically extract and cleanup your custom action binaries.

Batch file to "Script" a Database

Is it possible to somehow use a .bat file to script the schema and/or content of a SQL Server database?
I can do this via the wizard, but would like to streamline the creation of this file for source control purposes.
I would like to avoid the use of 3rd party tools, just limiting myself to the tools that come with SQL Server.
There is a free tool called SubCommander that is a part of the open source SubSonic software. I have successfully used this tool myself to create both schema and data "dumps" each night.
You can script out your schema and
data (and then version it in your
favorite source control system) using
SubCommander. Simply use the command
"version" and tell SubCommander where
to put the data:
sonic.exe version /out Scripts
This will output a script file (.sql)
to the local scripts directory of your
project
You can also try using the Microsoft SQL Server Database Publishing wizard, although i am not sure that you can use it in a bat file.

Resources