Share via


Database Creation Sample

This sample shows how to execute SQL statements from code.

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

  • Do one or more of the following:

    • On the Help menu, click Samples.

      The Readme displays information about samples.

    • Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.

    • Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.

For more information, see Visual Studio Samples.

Security noteSecurity Note:

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

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.

See Also

Tasks

How to: Install and Troubleshoot Database Components for Samples

Concepts

Comparison of ADO.NET and ADO

Reference

SqlCommand

Other Resources

ADO.NET Overview