Редагувати

Поділитися через


Bind controls to pictures from a database in .NET Framework applications

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.

You can use the Data Sources window to bind an image in a database to a control in your application. For example, you can bind an image to an Image control in a WPF application, or to a PictureBox control in a Windows Forms application.

Pictures in a database are typically stored as byte arrays. Items in the Data Sources window that are stored as byte arrays have their control type set to None by default, because byte arrays can contain anything from a simple array of bytes to the executable file of a large application. To create a data-bound control for a byte array item in the Data Sources window that represents an image, you must select the control to create.

The following procedure assumes that the Data Sources window is already populated with an item that is bound to your image.

To bind a picture in a database to a control

  1. Make sure that the design surface you want to add the control to is open in the WPF Designer or the Windows Forms Designer.

  2. In the Data Sources window, expand the desired table or object to display its columns or properties.

    Tip

    If the Data Sources window isn't open, open it by selecting View > Other Windows > Data Sources.

  3. Select the column or property that contains your image data, and select one of the following controls from its dropdown list control list:

    • If the WPF designer is open, select Image.

    • If the Windows Forms designer is open, select PictureBox.

    • Alternatively, you can select a different control that supports data binding and that can display images. If the control that you want to use is not in the list of available controls, you can add it to the list and then select it. For more information, see Add custom controls to the Data Sources window.