Building Database Application With VB 2010
-
Upload
jimmie-j-mshumbusi -
Category
Documents
-
view
221 -
download
0
Transcript of Building Database Application With VB 2010
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 1/16
1
Requiem to the VS 2003/2005/2008 Database Project
Posted Mon, Apr 19 2010 22:16 by Deborah Kurata
Sometimes a tool comes along that is so demure yet so useful and easy to use, it is very hard to
say good-bye. But our easy breezy Database project from Visual Studio 2003, Visual Studio2005, and Visual Studio 2008 is now gone. It was replaced with a new young tool that is more
full-featured, more complex, and somewhat aloof: the new Database project in VS 2010.
Several up-coming posts will detail the new tool. This post celebrates the life and times of the
old tool, paying our last respects.
If you are not familiar with the old Database project, it was a Visual Studio project type that youcould add to any solution. Here is the dialog in VS 2008. (It looks similar in VS 2005 and VS
2003.)
After clicking OK, you could define the connection associated with the Database project.
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 2/16
2
You could select one of the connections defined in the Server Explorer, or click Add New
Reference to define a new connection.
The purpose of the Database project was simply to hold your database scripts. You could easily
add any database script to this project, such as a create table script or stored procedure script.
You could then check these scripts into a source code control system along with the other sourcecode in your solution.
Once you added a Database project to your solution, the Server Explorer got additional options:
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 3/16
3
You could right-click on a table, stored procedure, or any scriptable object in Server Explorer,select Generate Create Script to Project, and the script for the selected object was automaticallyadded to the Database project in your solution.
The Database project shown below has both a Customer table script and a CustomerRetrieveAll
stored procedure script.
Once you had the scripts in your project, you could create new scripts or edit existing scripts likeany other code files. This feature was especially useful for creating and editing stored procedure
scripts.
Then you could select one or more scripts in Solution Explorer, right-click, and select Run to run
the scripts against the database defined as the default under the Database References.
So in just a click or two in Server Explorer you could generate the script for any database object
to your project. You could then work with the scripts like any other solution file. And with just aclick or two on the Database project files in Solution Explorer, you could apply the scripts back
to the database.
Simple and effective. Unintimidating and useful. You will be missed.
VS 2010 Database Project: An Introduction
Posted Sun, May 2 2010 17:54 by Deborah Kurata
Visual Studio 2010 has a new Database Project. The scripts in this new Database project define
your database just like the files in your code projects define your application.
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 4/16
4
The prior Database project (the one under the "Other Projects" node in VS 2003/2005/2008 as
detailed in this prior post), was simply a storage container for your database scripts.
The new Database project is alive. Like the old Database project, it retains your database scripts,but it also has Intellisense, build-time validation, and automatic deployment features. It allows
you to keep the definition of the database in your scripts and ensure that the scripts are deployedto your development database.
This is the first in a series of posts on using the new Database project. This post provides the
steps for adding a Database Project to any solution in either VB or C#.
1. Right-click on your solution and select Add | New Project. Or select File | New Project from
the main menu.
The Add New Project dialog is then displayed.
2. Open the Database node in the Installed Templates panel on the left and select SQL Server.
3. Select SQL Server 2008 Database Project from the middle panel, enter a name for the project,
and click OK.
NOTE: If you have SQL Server 2005, you can select the SQL Server 2005 Database Project
instead.
Visual Studio then creates the new project and adds it to your Solution Explorer:
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 5/16
5
4. Use the Database project to write the scripts for a new database or manage the scripts for anexisting database.
For example, use the Tables node to manage your table scripts and the Programmability | Stored
Procedures node to manage your stored procedure scripts:
These posts provide details on using this new Database project:
VS 2010 Database Project: Using an Existing Database
Posted Sun, May 2 2010 18:20 by Deborah Kurata
The new Visual Studio 2010 Database Project, as described in this prior post, makes it easy towork with scripts for an existing database directly within Visual Studio.
You can automatically build scripts for your existing tables, stored procedures, indexes,
constraints, and so on. You can then check these scripts into and out of source code control. You
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 6/16
6
can edit the scripts to change the table schemas or stored procedures. You can then build the
project to validate the scripts and then deploy the scripts back to the database.
Start by adding a VS 2010 Database project to your solution following the steps in this priorpost. Then follow the steps below to create the scripts in the project from an existing database.
1. Right-click on the Database project name in Solution Explorer and select Import DatabaseObjects and Settings.
The Import Database Wizard is displayed.
2. Select the database connection to the existing database, any desired properties, and click on
the Start button to start the wizard.
The database objects are then scripted directly into your Database project:
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 7/16
7
3. Click on the Finish button to finish.
View the Database project in Solution Explorer to see your added database objects.
Notice that this process added the three tables and three stored procedures from my database.
You can then check these files in and out of source code control just like any other project files.
You can double-click on any script to edit it.
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 8/16
8
When you are ready to apply the changes to your development database, you can deploy the
scripts as detailed in this post.
NOTE: If your database has been around a while, it is possible that some of the storedprocedures may have gotten out of synchronization with the tables. For example, if your DBA
renamed the CustomerInfo table to Customer, it is possible that your associated storedprocedures were not all modified to match. Unless your code calls all of the stored procedures,you may not have noticed the problem. Since the Database project is "alive", once you follow
this process to create the associated scripts into your Database project, any mismatches are
defined as application warnings:
This helps you ensure that your stored procedures and tables are always in synchronization.
If you want these types of warnings to be treated as application errors and prevent execution of
the application from within Visual Studio, you can change these warnings to errors. Double-click
on the Properties under the Database project in Solution Explorer and click on the Build tab.
Then check the "Treat Warnings as Errors" checkbox. Your warnings will immediately becomeapplication errors.
Use the techniques detailed here any time you are working with an existing database and want to
add the scripts to your solution.
VS 2010 Database Project: Adding Tables
Posted Sun, May 2 2010 22:43 by Deborah Kurata
The new Visual Studio 2010 Database Project, as described in this prior post, makes it easy to
work with scripts for an existing database or build new scripts directly within Visual Studio. Thispost details how to add table scripts to a Database project
Start by adding a VS 2010 Database project to your solution following the steps in this prior
post. Then follow the steps below to add tables to your project.
1. Right click on the Tables node for the Database project in Solution Explorer.
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 9/16
9
2. Select Add | Table.
This opens the Add New Item dialog:
3. Enter the name of the new table and click the Add button.
The table script template then appears in the code editor and you can enter the information for
the table.
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 10/16
10
Notice that there is no visual editor available here as there is in Server Explorer. But if you knowSQL, it is quick to enter the columns in the table.
When you are ready to apply the table script to your development database, you can deploy the
script as detailed in this post.
Use this technique any time you want to add a table script to your project.
VS 2010 Database Project: Adding Stored Procedures
Posted Sun, May 2 2010 23:37 by Deborah Kurata
The new Visual Studio 2010 Database Project, as described in this prior post, makes it easy towork with scripts for an existing database or build new scripts directly within Visual Studio. This
post details how to add stored procedure scripts to a Database project
Start by adding a VS 2010 Database project to your solution following the steps in this priorpost. Then follow the steps below to add stored procedure scripts to your project.
1. Right-click on the Stored Procedures node for the Database project in Solution Explorer:
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 11/16
11
2. Select Add | Stored Procedure
This opens the Add New Item dialog:
3. Enter the name of the new stored procedure and click the Add button.
The stored procedure template then appears in the code editor and you can enter the stored
procedure information.
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 12/16
12
One of the really nice new features in Visual Studio 2010 is full Intellisense when building the stored
procedures:
NOTE: To get full Intellisense on the available table fields, add the From clause first. Without
the From clause, Visual Studio does not know how to resolve the field list and provide
appropriate Intellisense.
NOTE: Intellisense would sometimes stop working for no reason I could figure out. Closing the
script and reopening it often helped get Intellisense working again.
When you are ready to apply the stored procedure script to your development database, you candeploy the script as detailed in this post.
But if you want to simply validate the stored procedure syntax before deployment, use theValidate SQL Syntax icon in the toolbar OR right-click on the code editor and select Validate
SQL Syntax. In either case, you will be asked to connect to your database. The syntax is then
checked and any errors are defined in the Messages tab at the bottom of the code window.
You can also execute the script, writing the stored procedure to the database, directly from the
code editor. Select the Execute SQL icon in the toolbar OR right-click on the code editor and
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 13/16
13
select Execute SQL. You will be asked to connect to your database before the script is executed
and the stored procedure is created in the defined database.
NOTE: Because the stored procedure scripts in the Database project are always Create Procedurescripts, you can not execute the script again once the stored procedure is created. The work
around for this is to change "Create Procedure" to "Alter Procedure" in the script, execute thescript, and change the script back to "Create Procedure".
You can also execute any SQL statements within the script by selecting the statements. Then
right-click and select Execute SQL. The script is then executed and the results are displayed inthe Results tab.
Use the techniques detailed in this post any time you want to add a stored procedure script to your
Database project or work with a stored procedure script in your project.
VS 2010 Database Project: Building and Deployment
Posted Sun, May 2 2010 22:52 by Deborah Kurata
The new Visual Studio 2010 Database Project, as described in this prior post, makes it easy to
manage the scripts for your database from within Visual Studio. This includes building theschema scripts from the individual database object scripts and deploying the scripts back to the
database.
One of the ways that the new VS 2010 Database project is significantly different from the
Database project available in prior versions of Visual Studio is the build and deployment
process.
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 14/16
14
The old VS 2003/2005/2008 Database project was just a container for your scripts. You could
right-click on the database in Server Explorer to generate the script to the project. When youmade changes to the script, you could right-click on the script and select to apply the script to the
database. But you had to manually perform these tasks.
And with the old style Database projects, it was easy to forget to apply your script changes to thedatabase. It also was not easy to keep everything in synchronization. For example, if you latermade a table change, it is easy to forget to adjust the related stored procedures as well.
Instead of being a simple repository, the new 2010 Database project is alive. The scripts in yourDatabase project define your database just like the files in your code projects define your
application. And the build and deployment process can apply the changes to your actual
database.
This post details how to use the VS 2010 Database project and deploy your scripts back to your
database.
Start by adding a VS 2010 Database project to your solution following the steps in this prior
post. Then write the scripts into your project from an existing database as detailed in this priorpost. Or write your own scripts in the project.
After you've made any changes to those scripts, you will want to deploy them back to thedatabase.
To build the Database project, right-click on the Database project and select Build. OR you can
use the Build menu.
When you build the Database project, Visual Studio validates your scripts and writes thedatabase schema to a file with a .dbschema extension.
To deploy the scripts to your database, right-click on the Database project and select Deploy. Oryou can use the Build menu.
When you deploy the Database project, Visual Studio builds a deployment script.
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 15/16
15
You can then run that one script to apply all of the information from your database object scriptsto the development database.
But if you want to ensure that your development database is always up to date with the scripts in
your Database project, you can set your Database project properties so that any changes are
automatically deployed to the development database when you run your application (or build thesolution).
Double-click on the Properties node of the Database project in Solution Explorer to open theDatabase project properties. Select the Deploy tab:
Set the Deploy action to "Create a deployment script (.sql) and deploy to the database". Then usethe Edit button to the right of the Target connection (cut off in the above screen shot) to set theconnection to your development database. Once you set these project settings, they will remain
set unless you change them.
Each time you deploy the Database project, using the menu options or by just running the
application within Visual Studio, Visual Studio will immediately apply all script changes directly
to the database.
8/3/2019 Building Database Application With VB 2010
http://slidepdf.com/reader/full/building-database-application-with-vb-2010 16/16
16
In this example, I added a table script for an Invoice table. When I ran the application, VisualStudio built and deployed the Database project scripts, creating the Invoice table in the database.
Use these features of the Database project any time you want to keep the master copy of yourdatabase schema defined in your scripts and ensure that the schema is in sync every time you run
your application within Visual Studio.