How to: Connect to a Database (C#)

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.
R @ S. Theme images by PLAINVIEW. Powered by Blogger.