PetaPOCO, MySQL & ASP.NET MVC

web.config settings-
<configuration>
  <connectionStrings>
    <add name="MySqlConnection" connectionString="Server=192.168.100.30;Port=3306;Database=grocery_db; User ID=root; Password=*****;" providerName="MySql.Data.MySqlClient" />   
  </connectionStrings>

 <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
</configuration>


Controller code-

public ActionResult Summary()
{
    //string connectionString
    GrocerySummaryViewModel viewModel = new GrocerySummaryViewModel();
    List<GroceryWiseContactQuantity> groceryContactList = new List<GroceryWiseContactQuantity>();
    try
    {
        var db = new PetaPoco.Database("MySqlConnection");
        groceryContactList = db.Query<GroceryWiseContactQuantity>("SELECT grocery AS GroceryName, count(id) AS Quantity FROM contacts group by grocery order by count(id) desc").ToList();
                
        viewModel.GroceryWiseContactList = groceryContactList;
                
    }
    catch (Exception Ex)
    {
        this.LogException(Ex);
    }
    return View("GrocerySummary", viewModel); 
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s