Visual Studio.NET Database Projects
- Creating a Database Project
- Database References
- Scripts
- Create Scripts
- Change Scripts
- Running the Scripts
- Command Files
- Queries
- Summary
The database project is a special type of Visual Studio.NET project. Its purpose is to create and manage SQL database scripts. If you're developing database applications with Visual Studio.NET, you will want to know about the tools available for making your work with databases easier and faster. This version of Visual Studio features many new tools and contains significant improvements to others that existed in previous versions.
The Visual Database tools allow us to view, design, modify, and test database objects (for example, tables, views, queries, stored procedures, and so on) quickly without having to jump from the Visual Studio environment to a different toolset. The main advantage is in design and development productivity, although licensing and installation issues also have been greatly simplified.
We have already worked with some of these tools in previous chapters, and their use is usually quite intuitive. They should also seem very familiar to you if you've had experience using similar tools (even MS Access qualifies). If this is your first time working with such tools, the Visual Studio help topics will be useful.
In this chapter we continue using some of the Visual Database tools to demonstrate how to utilize the tools and features available in a VS.NET database project.
Creating a Database Project
A special type of VS.NET project of particular interest is the database project. A database project is not specific to any .NET programming language, such as Visual Basic or C#, but is meant to be used to design, test, run, and manage SQL scripts and queries. When you design your application in multiple layers, as you should, this project type helps you to develop and manage the database layer closest to the database and the database itself.
In some applications, a database project may be part of the actual application code, and in other applications, it may be part of a separate administration solution used to set up and maintain the application's database.
To add a new database project to a solution, do the following.
Launch the Add New Project dialog from either the main File menu or from the context menu displayed by right-clicking on the solution in the Solution Explorer.
The left panel of this dialog box displays a list of folders containing different project types. Expand the Other Projects folder (click on the "+").
Select the Database Projects folder. It displays a Database Project template in the right panel of the dialog box.
Specify a project name of NoveltyData and a path for saving the project files and then click on the OK button.
If you don't currently have any database connections defined in the Server Explorer, the Data Link Properties dialog box will be displayed, allowing you to define a new database connection.
If you have at least one database connection defined in the Server Explorer, the Add Database Reference dialog box is displayed. From this dialog choose the database connection that you want to use (you can add new ones later). Alternatively, you can click on the Add New Reference button to display the Data Link Properties dialog box, allowing you to define a new database connection.
Select the connection to the Novelty database on the SQL Server and click on the OK button. If for some reason the connection doesn't exist, create it from the Data Link Properties dialog box.
Figure 8.1 shows the project and its folders in the Solution Explorer.
Figure 8.1 The NoveltyData database project shown in the Solution Explorer
Note that the created database project contains the following folders:
Change Scripts
Create Scripts
Queries
Database References
Let's take at look at these folders and what they contain. We start with Database References because they're the prerequisite for everything else.