.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; }
    }
}

.Net C# using the Dapper ORM

A quick look at the Dapper ORM and a simple example to grab some data from a SQL Server.

using System;
using System.Data.SqlClient;
using Dapper;

namespace CsharpDapperDemo
{
    /// <summary>
    /// Simple demonstration of the Dapper ORM.
    /// </summary>
    class CsharpDapperExample
    {
        static void Main()
        {
            const string myConnectionString = @"Data Source=MYSQLSERVER\SQLEXPRESS;Database=Company;Integrated Security=True;Connect Timeout=15;Encrypt=False";

            using (var connection = new SqlConnection(myConnectionString))
            {
                connection.Open();

                var companyDataResults  = connection.Query<CompanyData>("select * from dbo.EmployeeInfo");

                foreach (var result in companyDataResults)
                    Console.WriteLine("{0} {1} {2} {3} {4}", result.Empno, result.EmpName, result.Salary,
                                      result.DeptName, result.Designation);

                Console.ReadKey();
            }
        }

        /// <summary>
        /// Class for testing results from Dapper.
        /// </summary>
        private class CompanyData
        {
            public int Empno;
            public string EmpName;
            public int Salary;
            public string DeptName;
            public string Designation;
        }
    }
}