Ben Jasperson
Ben Jasperson

Reputation: 310

Create local SQL Server database from installation package for UWP

I'm trying to create an installation package that will create a local database for my UWP. Currently I'm using NSIS to create the package.

I developed my UWP with the idea that some users might want to use a tablet or phone from time to time, but the app will mainly be used on desktops and my installer is meant for desktop installations.

I first tried using the SQLLocalDB Utility , but I found out it's not supported in UWP. I eventually started using SQL Server media files to install SQL Server Express and I think that's the way I want to go (I want a local database that's a little more robust than SQLite and it seems to be my best option).

I don't know if my current issue is with NSIS or SQLCMD (which I use in the installer to build the database). Here's the part of my .nsi that I'm using to build the database:

OutFile "db_installer.exe"
!include psexec.nsh
!include LogicLib.nsh

Section

InitPluginsDir
SetOutPath $PLUGINSDIR
File /r SQLCMD 
ExecWait '"$PLUGINSDIR\SQLCMD\sqlcmd.exe" -S Cmp-000000\SQLEXPRESS -i createdbonfile.sql -o output.txt'
FileOpen $4 "$PLUGINSDIR\SQLCMD\output.txt" r
FileSeek $4 0
ClearErrors
${DoUntil} ${Errors}
    FileRead $4 $1
    DetailPrint $1
${LoopUntil} 1 = 0
FileClose $4

# default section end
SectionEnd

At one point I got the installer to at least print the SQL errors. Since then, I thought I only removed some calls to "DetailPrint". Now, however, SQLCMD still creates the output file but doesn't put anything in it. And of course the biggest issue is that my SQL commands are not being executed.

Upvotes: 0

Views: 317

Answers (2)

Ben Jasperson
Ben Jasperson

Reputation: 310

I was able to successfully build the local database I wanted by using the SQL Server media files and the SQLCMD utility (as mentioned in the question). The issue I had was with my .nsi code. I thought that I was reading the output.txt file I created using SQLCMD in the intaller, but it turns out I was actually reading a file I made in a practice run and accidently loaded to the installer while compiling. With that realization, it soon came obvious that I was missing the paths to the files I was referencing. Here's the code again with the appropriate files paths:

OutFile "db_installer.exe"
!include psexec.nsh
!include LogicLib.nsh

Section

InitPluginsDir
SetOutPath $PLUGINSDIR
File /r SQLCMD 
ExecWait '"$PLUGINSDIR\SQLCMD\sqlcmd.exe" -S Cmp-000000\SQLEXPRESS -i $PLUGINSDIR\SQLCMD\createdbonfile.sql -o $PLUGINSDIR\SQLCMD\output.txt'
FileOpen $4 "$PLUGINSDIR\SQLCMD\output.txt" r
FileSeek $4 0
ClearErrors
${DoUntil} ${Errors}
    FileRead $4 $1
    DetailPrint $1
${LoopUntil} 1 = 0
FileClose $4

# default section end
SectionEnd

Upvotes: 1

Martin Zikmund
Martin Zikmund

Reputation: 39082

You can connect to a remote SQL Server, for example on Azure. Creating LocalDB from UWP won't be supported, as it requires LocalDB to be installed on the target machine.

If you need a local database, then SQLite will be the best choice.

Upvotes: 0

Related Questions