Specify path for new SQL database - Advanced Installer Community
MichalH
Posts: 2
Joined: Thu Mar 24, 2022 3:57 pm

Specify path for new SQL database

Hello. Is it possible to specify path where new SQL server database should be created? If I use checkbox "Create database if it does not exist" in Predefined connection tab, database is created in default sql server directory (C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA) which is not desired. If I keep this checkbox unchecked and create custom sql script for creating db with specifying desired path for mdf and ldf files, I get an error "Cannot open database TestDB requested by the login. The login failed."
Catalin
Posts: 7852
Joined: Wed Jun 13, 2018 7:49 am

Re: Specify path for new SQL database

Hello and welcome to our forums,
If I use checkbox "Create database if it does not exist" in Predefined connection tab, database is created in default sql server directory (C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA)
Please note that this is the default behavior. The database will be created under the Server you selected in the "SQLServerConnectionDlg" dialog.
If I keep this checkbox unchecked and create custom sql script for creating db with specifying desired path for mdf and ldf files, I get an error "Cannot open database TestDB requested by the login. The login failed."
If possible, could you please create a sample project which contains your SQL Script and forward it to me so I can run some tests on my end?

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
MichalH
Posts: 2
Joined: Thu Mar 24, 2022 3:57 pm

Re: Specify path for new SQL database

Hello Catalin, I created small sample project showing how I had sql task configured before (attached).
In SQL Databases\Predefined connection I specified server + login + database name TestDB, keeping checkbox "Create database if it does not exist" unchecked and adding sql script to this connection for creating the database. (In this sample project, login user is not configured to be created.) With this configuration I got the mentioned error.
But since then I have found out that if in SQL Databases\Predefined connection\Catalogue I specify Master database instead of TestDB database and keep the rest same, TestDB is successfully created. Is this the correct way to do it?
Attachments
TestApplication.aip
(22.1 KiB) Downloaded 151 times
Catalin
Posts: 7852
Joined: Wed Jun 13, 2018 7:49 am

Re: Specify path for new SQL database

Hello,

I believe the issue, in your case, was that you did not have a database to run your script against.

In SQL Server Management Studio, if you want to run a query/script, you need to run it against an existing database (e.g. by right clicking on the database --> "New query".

In your case, you were trying to use the TestDB database, despite it not existing.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”