Database Project - how to set DatabaseName variable
Asked Answered
E

3

8

It's been a while sinse I used database projects and I'm tearing my hair out trying to figure out where to set the database name for this project.

I've tried in the SQLCMD variables section, but it doesn't let me (error below). I guess this section is to define your own variables (the same happens if I try $(DatabaseName))

enter image description here

The help link also doesn't work which is really helpful (goes to this dead link)

Earthshaker answered 17/12, 2015 at 9:14 Comment(0)
W
8

You can override DatabaseName using one of the following methods:

  1. When using Visual Studio's publish feature, there is a Database Name field that you can change within the dialog.
  2. When publishing via the command line, sqlpackage.exe takes a TargetDatabaseName argument. Example: sqlpackage.exe /Action:Publish /SourceFile:C:\DbProject\bin\Debug\DbProject.dacpac /TargetServerName:localhost /TargetDatabaseName:TestDb (See here for all sqlpackage.exe arguments)
Westphal answered 17/12, 2015 at 13:38 Comment(0)
L
10

I didn't want to use the publish feature but I did find a way to change the database name. It seems that the database name defaults to the project name. So, you can just rename the project to match your database name and that will do it. In my case, the project name and my desired database name are different. So, to change it, you can rename the project TWICE according to these steps:

  1. Rename the project to the name you want your database to be. When VS asks, "Do you also want to rename the target database for the project to 'xxx' ?", choose Yes.

  2. Rename the project back to what you want your project name to be. You'll get the same dialog but this time choose No.

This told me that the information is saved separately somewhere so I looked in the .sqlproj file and sure enough, there is a <TargetDatabase> element. So, another way to do it would be to just manually edit the .sqlproj file and change the TargetDatabase to your desired database name.

I'm using Visual Studio Community 2019.

Largely answered 7/8, 2019 at 16:18 Comment(4)
For VS 2019, this should be the accepted answer. I know it was added after the initial answer was accepted.Infract
Good trick, @Kyle. Works for Visual Studio Professional 2017.Perimorph
<TargetDatabase> element is not in my .sqlproj file as of Visual Studio Enterprise 2019 version 16.7.5. It appears the Element is now simply <Name>dbname</Name>Stet
Worked after I added that element in sqlproj. I'm using VS 2022 professional.Botfly
W
8

You can override DatabaseName using one of the following methods:

  1. When using Visual Studio's publish feature, there is a Database Name field that you can change within the dialog.
  2. When publishing via the command line, sqlpackage.exe takes a TargetDatabaseName argument. Example: sqlpackage.exe /Action:Publish /SourceFile:C:\DbProject\bin\Debug\DbProject.dacpac /TargetServerName:localhost /TargetDatabaseName:TestDb (See here for all sqlpackage.exe arguments)
Westphal answered 17/12, 2015 at 13:38 Comment(0)
D
4

I know this is an old topic, but I just encountered the problem.

Follow this steps to set DatabaseName variable:

  1. Unload Database project.
  2. Edit "TargetDatabase" element in the "<database_project_name>.sqlproj.user" file. If not exists, add it (e.g. "<TargetDatabase>MyDatabase</TargetDatabase>").
  3. Save .user file.
  4. Reload project.

I hope it helps.

Doubt answered 28/6, 2020 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.