Windows Form Application using Mysql Server


Here we will learn how to create a windows forms Data source from an Entity in an Entity Data Model.

For Example : We have a database whose name is 'mcn' and a window form in .Net Framework3.5.\ and how to install database on the same page.
We have to follow some steps which as given below :

Create a new window form application
  • Go in visual studio, select file>new>project> in main menu.
  • Choose the windows forms application. Click Ok .The solution is created.

Adding an Entity Data Model 

  1. In the solution Explorer ,right click your application and select >Add>New Item. Form Visual Studio installed templates select Ado.net entity data model. Click Add.

    img 1.gif

  2. See the entity data model wizard to show the entity data model from the mcn database.

    img 2.gif

  3. Select the connection we have to made earlier to the mcn database. if not to be ready done so, we can create the new connection at that time by clicking New connection.

    img 3.gif

  4. Click Next.

  5. The Entity  data model wizard connects to the database .See the tree structure of the database. select the object we would like to include in our model. If we had created views and stored routines these will be displayed along with any tables. here we have to need to select the tables.

    Click Finish to create the model and exit the wizard.

    img 4.gif

  6. Visual Studio will generate the model and then display it.

    img 5.gif

  7. From the visual studio main menu select Build, Build Solution.

Adding a new data source

Now add a new data source in project and see how it can be used to read and write database.

  1. In visual studio main menu select Data, Add New Data Source . see the Data Source Configuration Wizard.

    img 6.gif

  2. Select the Object icon .Click Next.

  3. Select the object to bind .Expand the tree . Select the persons table and click Next.

    img 7.gif

  4. The wizard will confirm that the city object is to be added. Click Finish.

    img 8.gif

  5. The persons object will be display in data source panel.

    img 9.gif


Using the Data Source in a windows Form

To use the data source in a window form follow the steps:

  1. In the data source panel >select data source and drag & drop it on to the form designer.By default the data source object will be added as a data grid view control.it bind the personsBindingsource and personsBindingNavigator.

    img 10.gif

  2. Save and rebuild the solution before continuing.

Adding code to populate the data grid view

The data grid view control will be populated with data from the persons database tables.

  1. Double click on the form to access it's code
  2. Add code to instatiate the entity data model container object and retrieve data from the database

    img 11.gif

  3. Save and rebuild the solution

  4. Run the solution

    img 12.gif

Adding Code to Save Changes to the Database

Now add code to enable to save changes to the database. The binding source component ensures that changes made in the data grid view control are also made to the entity classes bound to it.

  1. In the form designer, click the save icon in the form toolbar and ensure that its enabled property is set to true.

    img 123.gif

  2. Double click to the save icon in the form toolbar to display its code.

  3. Need to add code to ensure that data is saved to the database when the save button is clicked in the application.

    img ado.gif

Resources 

Here are some useful related resources:


Similar Articles