Insert with Dapper Micro ORM and ASP.NET MVC 3

No.of Views2188
Bookmarked0 times
Downloads 
Votes0
By  jalpesh   On  13 Jun 2011 04:06:12
Tag : ASP.NET , ASP.NET MVC
In this article, I am going to explain how we can insert data with the dapper ORM. So let’s extend the earlier post project. As explained in earlier post I have already created a class called CustomerDB this class will contains all the operation with Dapper Micro ORM. So For inserting data let’s first create CREATE method like following in CutomerDB Class like following. In that I have create a simple Insert Query in string and then using connection.execute method to execute method. Following is
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article, I am going to explain how we can insert data with the dapper ORM. So let’s extend the earlier post project. As explained in earlier post I have already created a class called CustomerDB this class will contains all the operation with Dapper Micro ORM. So For inserting data let’s first create CREATE method like following in CutomerDB Class like following. In that I have create a simple Insert Query in string and then using connection.execute method to execute method. Following is code for that.

http://www.codegain.com/articles/aspnet/aspnetmvc/workaround-with-dapper-micro-orm-and-aspnet-mvc-3-0.aspx

Implementation

C# Code

public class CustomerDB
{
   public string Connectionstring = @"Data Source=DotNetJalps\SQLExpress;Initial Catalog=CodeBase;Integrated Security=True";
 
   public IEnumerable<Customer> GetCustomers()
   {
       using (System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(Connectionstring))
       {
           sqlConnection.Open();
           var customer = sqlConnection.Query<Customer>("Select * from Customer");
           return customer;
 
       }
   }
 
      
   public string  Create(Customer customerEntity)
   {
       try
       {
           using (System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(Connectionstring))
           {
               sqlConnection.Open();
              
               string sqlQuery = "INSERT INTO [dbo].[Customer]([FirstName],[LastName],[Address],[City]) VALUES (@FirstName,@LastName,@Address,@City)";
               sqlConnection.Execute(sqlQuery,
                                     new
                                         {
                                             customerEntity.FirstName,
                                             customerEntity.LastName,
                                             customerEntity.Address,
                                             customerEntity.City
                                         });
 
               sqlConnection.Execute(sqlQuery);
               sqlConnection.Close();
 
           }
           return "Created";
       }
       catch (Exception ex)
       {
           return ex.Message;
       }
 
   }
 
}

Now we are ready with Create Method for database now let’s create two ActionResult for the Creating customer like following in Customer Controller like following.

public ActionResult Create()
{
 return View();
}
 
//
// POST: /Customer/Create
 
[HttpPost]
public ActionResult Create(Customer customer)
{
 try
 {
     // TODO: Add insert logic here
     var customerEntities = new CustomerDB();
     customerEntities.Create(customer);
     return RedirectToAction("Index");
 }
 catch
 {
     return View();
 }
}

Now we are ready with the both ActionResult. First ActionResult will return simple view of Create which we are going to create now and another ActionResult Create will get customer object from the form submitted and will call our create method of CustomerDB Class. Now it’s time to create a view for adding customer. Right Click return view Statement in Create Action Result and Click Add View and Just Create view like following.

 

Image Loading

That’s it now we are ready. Now let’s test it in browser. Like following.

Image Loading

Now let’s click and create and then it will redirect us to customer list page like following. Where you can see the newly added details.
 

Image Loading

So that’s it. Its very easy to Insert data with Dapper Micro ORM. Hope you like it and you can find part one here

 
Sign Up to vote for this article
 
About Author
 
jalpesh
Occupation-Software Engineer
Company-DotNetJaps
Member Type-Gold
Location-India
Joined date-08 May 2010
Home Page-http://www.dotnetjalps.com
Blog Page-http://www.dotnetjalps.com
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</