Database Creation Sample

SQL statements are executed that create a database, a table, a stored procedure, and a view in code. SQL statements are then executed that populate the table with data from the Northwind database. This table is then queried to fill a DataSet that is subsequently bound to a DataGrid for display.

To get samples and instructions for installing them, see the following:

  • Click Samples on the Visual Studio Help menu.

    For more information, see Locating Sample Files.

  • The most recent versions and the complete list of samples are available on the Visual Studio 2008 Samples Web site.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied to a folder under \Program Files\Visual Studio 9\Samples\. For Visual Studio Express Editions, all samples are located on the Internet.

To run this sample

  1. Press F5.

  2. If you want to run the application again after you create the database, you will have to close all applications that maintain an active connection to the instance of SQL Server that contains the demo database. This means you will have to close and reopen Visual Studio if you opened a connection to the database in the Server Explorer. Other applications, such as SQL Query Analyzer, may also hold an open connection. Alternatively, you can stop and restart the instance of SQL Server by using the SQL Server Service Manager in the system tray.

Requirements

This sample requires the Northwind database. For more information, see How to: Install and Troubleshoot Database Components for Samples.

Demonstrates

The application leads you through the steps in creating and populating the database:

  • Create the database   The SqlCommand class uses the CREATE DATABASE statement to create the How to Demo database.

  • Create the data table   The SqlCommand class uses the CREATE TABLE statement to create a table named NW_Seafood. The table includes fields for product ID, product name, quantity per unit, and unit price.

  • Create a stored procedure   The SqlCommand class uses the INSERT INTO statement and the SELECT statement to add code to the new table and retrieve the data from the table.

  • Create a view   The SqlCommand class uses the CREATE VIEW statement to select rows in the NW_Seafood table.

  • Populate the table   The SqlCommand class uses the EXECUTE statement to run a procedure that retrieves rows from the Northwind database and add them to the NW_Seafood table.

  • Display the data in the table   The SqlCommand class uses the SELECT statement to fill a DataSet that is then used as the DataSource of a DataGrid control. Table and column style objects are used to customize the appearance of the DataGrid.