Share via


How to: Connect to a Database (C#)

Connecting to a data source is easy when you use Visual C# Express Edition. You can use visual tools to browse to a database and add a local copy to your project. When you install SQL Server Compact 3.5 with Visual C# Express Edition, you have access to a sample database named Northwind.sdf. You can connect to the Northwind database by using the Data Sources window.

Important noteImportant Note:

You have to run Visual C# Express Edition as administrator to gain access to the Northwind database. On the Start menu, right-click Visual C# Express Edition and then click Run as administrator.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

To add a connection to the Northwind database

  1. On the File menu, point to New and then click Project.

  2. In the New Project dialog box, click Windows Forms Application, and then click OK.

    A new Windows Forms project opens.

  3. On the Data menu, click Add New Data Source.

    The Data Source Configuration Wizard opens.

  4. Click Database and then click Next.

  5. Click New Connection.

    The Add Connection dialog box opens.

  6. In the Add Connection dialog box, if the Data source is not Microsoft SQL Server Compact 3.5 (.NET Framework Data Provider for Microsoft SQL Server Compact 3.5), click Change and select Microsoft SQL Server Compact 3.5 from the Change Data Source dialog box. Click OK.

  7. Click the Browse button and locate the Northwind.sdf database.

    The default location is drive:\Program Files\Microsoft SQL Server Compact Edition\v 3.5\Samples.

  8. Click Test Connection to verify that the connection succeeded, and then click OK to close the dialog box.

  9. Click OK to close the dialog box, and then in the Data Source Configuration Wizard, click Next. If prompted to copy the data file to your project, click Yes.

  10. On the next page of the wizard, select the Yes, save the connection as check box, and then click Next.

  11. On the Choose Your Database Objects page, expand Tables.

  12. Select the check box for both Products and Order Details, and then click Finish.

    A local database file is added to your project. Notice that a NorthwindDataSet object has been added to the Data Sources window.

  13. In Solution Explorer, select Northwind.sdf and make sure that the Copy to Output Directory property is set to Copy if newer.

  14. On the File menu, click Save All to save your project.

See Also

Other Resources

Connecting to Data (Visual C#)

Visual C# Guided Tour

SQL Server Compact 3.5 Books Online

Change History

Date

History

Reason

October 2008

Added a note about running Visual C# Express Edition as administrator.

Customer feedback.