Szerkesztés

Megosztás a következőn keresztül:


Add data sources in ADO.NET applications (.NET Framework)

When you work with .NET data tools in Visual Studio, you can add data sources (.NET objects) to connect your .NET application to information in data stores. The Visual Studio designers can consume the data source output to generate boilerplate code that binds the data to forms when you drag and drop database objects from the Data Sources window.

Examples of data sources include:

  • A class in an Entity Framework model associated with some kind of database

  • A dataset associated with some kind of database

  • A class that represents a network service, such as a Windows Communication Foundation (WCF) data service or a REST service

  • A class that represents a SharePoint service

  • A class or collection in your solution

Note

Datasets and related classes are legacy .NET Framework technologies from the early 2000s that enable applications to work with data in memory while the applications are disconnected from the database. The technologies are especially useful for applications that enable users to modify data and persist the changes back to the database. Although datasets have proven to be a very successful technology, we recommend that new .NET applications use Entity Framework Core. Entity Framework provides a more natural way to work with tabular data as object models, and it has a simpler programming interface.

Prerequisites

  • An application that implements Windows Forms or Windows Presentation Format (WPF) objects that target .NET Framework. You can create this type of application by using a template in Visual Studio when your installation includes the .NET Desktop development workload. For more information, see Modify Visual Studio workloads, components, and language packs.

    Important

    The features described in this article aren't supported for .NET Core development for WPF or Windows Forms.

  • The application must use data-binding features: datasets, the Entity Framework, the Language Integrated Query (LINQ) to SQL, a WCF data service, or SharePoint.

    Tip

    When the application doesn't use one or more of these components, the concept of a "data source" doesn't apply. In these scenarios, you can connect directly to the database by using SqlCommand class objects.

Work with data sources and Windows forms

You create and edit data sources for .NET Framework by using the Data Source Configuration Wizard in a Windows Forms or WPF application. You can create a data source from a database, a service, an object, or from a SharePoint list:

Screenshot that shows the Data Source Configuration Wizard in Visual Studio.

After you create data sources, they're visible in the Data Sources window. When your project is open in Visual Studio, you can access this window by selecting View > Other Windows > Data Sources. You can also use the Shift+Alt+D keyboard shortcut.

In a Windows Forms project that targets .NET Framework (not .NET Core or .NET 5 or later), you can drag a data source from the Data Sources window onto a Windows form design surface or control. Some examples of how you can connect a data source with a form include:

  • Drag a table to get a BindingNavigator and a DataGridView for the table.

  • Drag a specific column to get a BindingNavigator with a Label and TextBox for the column.

  • Drag all the table columns to get a complete data viewing form for the table.

These actions trigger Visual Studio to generate boilerplate code that displays the data from the data store. The following example shows a dataset dropped onto a Windows form. If you select F5 on the application, the data from the underlying database appears in the form's controls.

Screenshot that shows how to drag a dataset onto a Windows form in Visual Studio 2022.

Screenshot that shows how to drag a dataset onto a Windows form in Visual Studio 2019 and earlier.

Create data source from database or database file

You can create a dataset or an Entity Framework model to use as a data source for a database or database file with the Data Source Configuration Wizard. If your configuration uses Entity Framework, first create your Entity classes, and then use the wizard to create data sources.

Create dataset as data source

The Data Source Configuration Wizard provides the option to create a data source by connecting to a database. You can choose the database objects to use as the dataset for your application.

Follow these steps to create a dataset as a data source:

  1. In Visual Studio, select Project > Add New Data Source to open the Data Source Configuration Wizard.

  2. For the type of data source, select Database.

  3. For the database model, select Dataset.

  4. Follow the prompts to specify a new or existing database connection, or a database file.

  5. Select the database objects to include in the dataset, and complete the wizard.

For detailed instructions to complete the wizard, see Create and configure datasets in .NET Framework with Visual Studio.

Create Entity Framework model as data source

When your configuration uses Entity Framework, you first create your Entity classes and then use the Data Source Configuration Wizard to create the data source.

Follow these steps to create an Entity Framework model as a data source with the Entity Data Model Wizard:

  1. Select Project > Add New Item. The Add New Item dialog opens.

  2. In the dialog, select the ADO.NET Entity Data Model template, enter a name for the model instance, and select Add:

    Screenshot that shows how to create a new ADO.NET Entity Data Model by using the Add New Item dialog.

    The Entity Data Model Wizard opens.

  3. In the wizard, choose the method to generate the Entity Framework model:

    • EF Designer from database: Creates a model in the Entity Framework Designer based on an existing database. You can choose the database connection, settings for the model, and database objects to include in the model. The classes your application interacts with are generated from the model.

    • Empty EF Designer model: Creates an empty model in the Entity Framework Designer as a starting point for visually designing your model. Later, you can generate a database from your model. The classes your application interacts with are generated from the model.

    • Empty Code First model: Creates an empty Code First model as a starting point for designing your model by using code. Later, you can generate a database from your model.

    • Code First from database: Creates a Code First model based on an existing database. You can choose the database connection, settings for the model, and database objects to include in the model.

    Screenshot that shows the options for generating the new model in the Entity Data Model wizard.

    After you make your choice, select Next to continue in the wizard.

  4. Add the model as the data source for your application, and complete the wizard.

    For detailed instructions to complete the wizard, see Create Model Classes with the Entity Framework (C#).

  5. After you generate classes for the model, you can create a data source from an object with the Data Source Configuration Wizard.

    • Select the tables (or individual columns), stored procedures, functions, and views from the model for use in the dataset.

      Screenshot that shows the generated Entity classes for the model in the Data Source Configuration Wizard.

    For detailed instructions, see Create data source from object.

Create data source from service

The Data Source Configuration Wizard supports creating a data source from a service. In this approach, Visual Studio adds a service reference to your project and creates proxy objects that correspond to objects returned by the service. When a service returns a dataset, the service is represented in your project as a dataset. If the service returns a specific type, the service is represented in your project as the returned type.

You can create a data source from the following types of services:

Note

Items in the Data Sources window are dependent on the data returned by the service. Some services might not provide enough information for the Data Source Configuration Wizard to create bindable objects. After you complete the wizard, if the service returns an untyped dataset, no items appear in the Data Sources window. Untyped datasets don't provide a schema, so the wizard doesn't have enough information to create the data source.

Follow these steps to create a data source from a service:

  1. In Visual Studio, select Project > Add New Data Source to open the Data Source Configuration Wizard.

  2. For the type of data source, select Service.

    The Add Service Reference dialog opens. You can also access this dialog by right-clicking your project in Solution Explorer and selecting Add service reference.

For detailed instructions to complete the wizard, see Create and configure datasets in .NET Framework with Visual Studio.

Create data source from object

The Data Source Configuration Wizard also lets you create a data source from any object that exposes one or more public properties.

All public properties of an object are visible in the Data Sources window. If you're using Entity Framework with a generated model, this window shows the entity classes that are the data sources for your application.

Note

To support drag-and-drop data binding, objects that implement the ITypedList or IListSource interface must have a default constructor. Otherwise, Visual Studio can't instantiate the data-source object and shows an error when you drag the item to the design surface.

Follow these steps to create a data source from an object:

  1. In Visual Studio, select Project > Add New Data Source to open the Data Source Configuration Wizard.

  2. For the type of data source, select Object.

  3. On the Select the Data Objects page, expand the nodes in the tree view to locate the objects that you want to bind to.

    The tree view contains nodes for your project and for assemblies and other projects referenced by your project.

    1. To bind to an object in an assembly or project that doesn't appear in the tree view, select Add Reference.

    2. Use the Reference Manager dialog to add a reference to the assembly or project. After you add the reference, Visual Studio adds the assembly or project to the tree view.

    Note

    You might need to build the project that contains your objects before the objects appear in the tree view.

For detailed instructions to complete the wizard, see Create and configure datasets in .NET Framework with Visual Studio.

Create data source from SharePoint list

You can also use the Data Source Configuration Wizard to crate a data source from a SharePoint list. SharePoint exposes data through WCF Data Services, so creating a SharePoint data source is the same as creating a data source from a service.

Note

This approach requires the SharePoint SDK.

Follow these steps to create a data source from a SharePoint list:

  1. In Visual Studio, select Project > Add New Data Source to open the Data Source Configuration Wizard.

  2. For the type of data source, select SharePoint.

  3. Use the Add Service Reference dialog to connect to the SharePoint data service by pointing to the SharePoint Server.

For detailed instructions to complete the wizard, see Create and configure datasets in .NET Framework with Visual Studio.