Showing posts with label .Net Framework. Show all posts
Showing posts with label .Net Framework. Show all posts

Tuesday, January 3, 2012

Start using .Net Entity Framework - 3


This article illustrates the usage of ADO.Net Entity Framework in creating, updating and deleting data records.

This article comes after:
    - Start using .Net Entity Framework - 1 , and
    - Start using .Net Entity Framework - 2

where we saw how to create a data model (ADO.Net Entity Data Model) from a pre-existing database, and how to use that model to query the database and fill a comboBox, by using a small example. The example database has only one table (Pais) with countries.

Taking the same example, we added a label to Form1, which will enable the visualization of each record's ID, and a text box that enables the user to modify the name of a country or create a new country in the database.



Buttons have also been added to Create New, Update and Delete a country in the table, and a Cancel button that closes the application.

To start, we add, in the method that handles the selection of a new line in the comboBox, the code to  fill the label (label1) with the country ID, and the text box (paisTextBox) with the name (nome) of the country. This allows the user to edit the current name of a country, in order to modify it, or to insert a new country's name, in order to create it in the database.

        private void paisesBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label1.Text = paisesBox1.SelectedValue.ToString();
           
            paisAtual = (Pais)paisesBox1.SelectedItem;
            paisTextBox.Text = paisAtual.nome;
        }

Thursday, December 29, 2011

Start using .Net Entity Framework - 2

After having created the Windows Forms Application project and generated the data model from the database let's see how we can access the database through the model, using a small illustrative example.

In the Form1 form, created automatically when creating the Windows Forms Application project, we will create a comboBox, dragging it from the toolbox.


In the Properties tab we change its name to paisesBox1.


Start using .Net Entity Framework - 1

This article illustrates the creation of an Entity Framework model and its use with a small program in C # for Windows (Windows Application).
To implement this example you must have installed Visual Studio 2010 (with 2008 should also work), as well as .Net framework 3.5 SP1 or higher.
In addition, it is necessary to access a database
service, so as the first approach, it is advisable to use Microsoft SQL Server 2008.

1. The first step is to create a new project of type
Windows Forms Application.

ADO.Net Entity Framework

Entity Framework is a set of ADO.NET technologies that support the data-oriented software development.

Typically, today, developers of data-driven applications perform the
modelling of entities and relationships in an object-oriented way, and then have to deal with the format in which these entities have to be put in a database engine. The data can be physically distributed across multiple storage systems and even applications that work with only one database system have to make trade-offs between the logical format of the modelled data, present in the data model, and the physical format, easy to maintain and oriented to an efficient access.

Entity Framework enables developers to work with data in the form of domain-specific objects, as modelled in the
data abstract model, without worrying about the tables of the database where they are stored.

With Entity Framework, developers can work at an abstraction
level higher than when they have to deal directly with the data, and can create and maintain data-driven applications with less code than with traditional applications.

As Entity Framework is a component of the .NET Framework, applications that use Entity Framework can run on any computer that has .Net framework version 3.5 SP1 or above
installed.

In the next article we will see a small example of how to use ADO.Net Entity Framework.


Other related articles:
    - Start using .Net Entity Framework - 1
    - Start using .Net Entity Framework - 2