.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
satyapriyanayak
Subhransu Sekhar Jena
abhays
SP
DotnetUser

Home >> Articles >> Azure >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Azure Part 30 - SQL Azure - Using ADO.NET Entity Framework - Data Operations

Posted By:Jean Paul       Posted Date: February 06, 2012    Points: 200    Category: Azure    URL: http://www.jeanpaulva.com/  

In this article we are going to explore data operations using ADO.NET Entity Framework over our SQL Azure database. The data model creation which is an essential part required was discussed in the previous article.
 

In this article we are going to explore data operations using ADO.NET Entity Framework over our SQL Azure database.  The data model creation which is an essential part required was discussed in the previous article. 

Pre-Requisites 

For working on this application, you need to have the following pre-requisites.

 

  • Azure project with web role
  • Entity data model with Employee class

 

You may use the project from the previous article.

 

After having the above, place the following on the Default.aspx page

 

  • 4 buttons with labels Insert, Update, Delete, Refresh
  • A grid control

 

 

Insert Operation

 

For creating a new employee, use the following code in the Insert button click event.

 

mysqlazureEntities context = new mysqlazureEntities();

 

Employee employe = new Employee();

employe.Name = "New Employee from EF";

 

context.AddToEmployees(employe);

context.SaveChanges();

Update Operation

 

For creating a new employee, use the following code in the Update button click event.

 

mysqlazureEntities context = new mysqlazureEntities();

Employee employee = context.Employees.Where(emp => emp.Name == "New Employee from EF").FirstOrDefault();

 

if (employee != null)

    employee.Name = "Updated Name";

 

context.SaveChanges();

 

Delete Operation

 

For creating a new employee, use the following code in the Delete button click event.

 

mysqlazureEntities context = new mysqlazureEntities();

Employee employee = context.Employees.Where(emp => (emp.Name == "New Employee from EF" || emp.Name=="Updated Name")).FirstOrDefault();

 

if (employee != null)

    context.DeleteObject(employee);

 

context.SaveChanges();

 

Select Operation

 

For creating a new employee, use the following code in the Refresh button click event.

 

GridView grid = GridView1;

 

grid.Columns.Clear();

grid.Columns.Add(new BoundField() { HeaderText = "Id", DataField = "Id" });

grid.Columns.Add(new BoundField() { HeaderText = "Name", DataField = "Name" });

 

mysqlazureEntities context = new mysqlazureEntities();

grid.DataSource = context.Employees.ToList();

grid.DataBind();

 

On executing the application and playing with the buttons you can view the following results.

 

 

Summary

 

In this article we have explored the data operations using ADO.NET Entity Framework over our SQL Azure database.  You need to change the database name, user name and password in the configuration file.

 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend