.Net C# PetaPoco inserting a row

I’ve been experimenting with the PetaPoco framework again. Below is an update to the earlier example with a simple row insertion routine.

using System;
using System.Linq;
using PetaPoco;

namespace CsharpPetaPocoDemo
{
    /// <summary>
    /// Simple PetaPoco example
    /// </summary>
    class CsharpPetaPocoExample
    {
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        static void Main()
        {
            // Nuget Install-Package PetaPoco

            using (var db = new Database("PetaExample"))
            {
                try
                {
                    // Insert a new data row
                    var employee = new EmployeeInfo
                                       {
                                           EmpName = "Person d",
                                           Salary = 10000,
                                           DeptName = "Service",
                                           Designation = "Supervisor"
                                       };

                    db.Insert("EmployeeInfo", "EmpNo", employee);

                    // Select the rows
                    var result = db.Query<EmployeeInfo>("select * from EmployeeInfo").ToList();

                    result.ForEach(PrintResults);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            Console.ReadKey();
        }

        private static void PrintResults(EmployeeInfo employeeInfo)
        {
            Console.WriteLine("{0} {1} {2} {3} {4}", employeeInfo.EmpNo, employeeInfo.EmpName, employeeInfo.Designation, employeeInfo.DeptName, employeeInfo.Salary);
        }
    }

    /// <summary>
    /// Employee class, using properties for PetaPoco
    /// </summary>
    [TableName("EmployeeInfo")]
    [PrimaryKey("EmpNo")]
    public class EmployeeInfo
    {
        public int EmpNo { get; set; }
        public string EmpName { get; set; }
        public int Salary { get; set; }
        public string DeptName { get; set; }
        public string Designation { get; set; }
    }
}

Returns:

1 Person a Agent Sales 20000
2 Person b Officer Security 30000
3 Person c Geek IT 40000
4 Person d Supervisor Service 10000

.Net C# using the PetaPoco Micro ORM

Micro ORMS are increasingly popular because of their simplicity and generally lower overhead compared to monolithic ORM frameworks. PetaPoco is a commonly used Micro ORM, the code below shows how to get up and running.

1) Create a new project in Visual Studio (2012 in this example).

2) Add thePetaPoco reference to your project, if you’re using nuget from the command line do:

install-package PetaPoco

3) Add a connection string setting to your config file:

<connectionStrings>
    <add name="PetaExample" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Company;Integrated Security=True;Connect Timeout=300;" providerName="System.Data.SqlClient" />
</connectionStrings>

4) Create a new class CsharpPetaPocoDemo.cs with the following contents:

using System;
using System.Linq;
using PetaPoco;

namespace CsharpPetaPocoDemo
{
    /// <summary>
    /// Simple PetaPoco example
    /// </summary>
    class CsharpPetaPocoExample
    {
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        static void Main()
        {
            using (var db = new Database("PetaExample"))
            {
                try
                {
                    var result = db.Query<EmployeeInfo>("select * from EmployeeInfo").ToList();

                    result.ForEach(PrintResults);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            Console.ReadKey();
        }

        private static void PrintResults(EmployeeInfo employeeInfo)
        {
            Console.WriteLine("{0} {1} {2} {3} {4}", employeeInfo.EmpNo, employeeInfo.EmpName, employeeInfo.Designation, employeeInfo.DeptName, employeeInfo.Salary);
        }
    }

    /// <summary>
    /// Employee class, using properties for PetaPoco
    /// </summary>
    [TableName("EmployeeInfo")]
    [PrimaryKey("EmpNo")]
    public class EmployeeInfo
    {
        public int EmpNo { get; set; }
        public string EmpName { get; set; }
        public int Salary { get; set; }
        public string DeptName { get; set; }
        public string Designation { get; set; }
    }
}