Aracılığıyla paylaş


How to: Connect to Data in a SQL Server Express Database

You can establish communication between your application and a SQL Server Express database by creating a connection that points to the database's .mdf file. You connect to data in SQL Server Express database files by running the Data Source Configuration Wizard and selecting Database on the Choose a Data Source Type page.

Note

To create a SQL Server Express database file, see the following page: Walkthrough: Creating a SQL Server Express Database.

You start the wizard by selecting the Add New Data Source command from the Data menu or from within the Data Sources window. Alternatively, you can choose the Add Existing Item command from the Project menu and browse to the desired database file.

Tip

Drag an .mdf or .mdb file from Windows Explorer into Solution Explorer to automatically configure your connection. This action also starts the Data Source Configuration Wizard ready for you to select the objects to use in your application.

After completing the wizard, a copy of the database (.mdf file) and a strongly typed dataset file (.xsd) are added to your project and the selected database objects are immediately available in the Data Sources Window for dragging onto your form. For more information, see Displaying Data Overview.

Note

When adding the .mdf file through the Data Source Configuration Wizard, you will be given the choice of adding the file to your project or leaving the file in its original location. For more information, see How to: Manage Local Data Files in Your Project.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To create a new data source from a SQL Server database file using the Add New Data Source command

  1. On the Data menu, choose Add New Data Source.

  2. Select Database on the Choose a Data Source Type page.

  3. Select New Connection to create a new data connection.

    Note

    If the Data source is not Microsoft SQL Server Database File, then select Change to open the Choose/Change Data Source dialog box.

  4. In the Choose Data Source dialog box, select Microsoft SQL Server Database File. Click OK.

    The default provider is .NET Framework Provider for SQL Server. For more information, see Choose/Change Data Source Dialog Box.

  5. Enter the path to the database file you want to access, or click the Browse button to locate the database file.

  6. Select the authentication mode and login information required by your database. For more information, see Add/Modify Connection (Microsoft SQL Server Database File).

  7. Click OK, and then click Next.

Note

When adding the .mdf file through the Data Source Configuration Wizard, you will be given the choice of adding the file to your project or leaving the file in its original location. For more information, see How to: Manage Local Data Files in Your Project.

  1. Click Next.

    Note

    By default the connection is saved in the application configuration file. Clear the check box if you want to save the connection string directly in the compiled application. For more information, see How to: Save a Connection String.

  2. Select the database objects to use in your application.

  3. Replace the default dataset name if you want to.

  4. Click Finish.

    The data source you just created is now available in the Data Sources window.

To create a new data source from a SQL Server database file using the Add Existing Item command

  1. On the Project menu, choose Add Existing Item.

  2. Select Data Files in the Files of type drop-down list.

  3. Browse to the database (.mdf) file that you want.

  4. Click Add.

  5. Select the database objects to use in your application.

  6. Replace the default dataset name if desired.

  7. Click Finish.

    The data source you just created is now available in the Data Sources window.

Next Steps

To add functionality to your application

  • Select items in the Data Sources window and drag them onto a form. For more information, see Displaying Data Overview.

    Note

    If the data source does not appear in the Data Sources window, click the Refresh button in the window.

See Also

Tasks

How to: Manage Local Data Files in Your Project

Local Data Sample

Walkthrough: Creating a SQL Server Express Database

Walkthrough: Displaying Data on a Form in a Windows Application

How to: Connect to Data in a Database

How to: Connect to Data in a Web Service

How to: Connect to Data in an Object

How to: Connect to Data in an Access Database

Concepts

Local Data Overview

Dataset Designer

Data Sources Overview