Simple.Data – introduction

I want to show you simple library to accessing and modyfing database. Simple.Data is some kind of ORM but very lightweight ORM. In my opinion it could be use in simple projects.

I’ll create vary simple application (I’ll use SqlCompact) and if you want to know more you could get more information here. Let’s start 🙂
So, I’m creating Console Application and installing all necessary libraries by NuGet:

After that, I’m creating database:


After that, I’m adding connection string to App.config:

<!--?xml version="1.0" encoding="utf-8"?-->
<configuration>
  <connectionstrings>
    <add connectionstring="Data Source=MyDatabase.sdf;Persist Security Info=False;" name="MyConnectionSring"></add>
  </connectionstrings>
  <startup>
    <supportedruntime sku=".NETFramework,Version=v4.5" version="v4.0"></supportedruntime>
  </startup>
</configuration>

After that, I’m adding stars to database and next I’ll get them from database:
using Simple.Data;
using System;
 
namespace Letys.SimpleDataExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var db = Database.OpenNamedConnection("MyConnectionSring");
 
            db.Stars.Insert(Name: "Sun", Hydrogen: 73.46);
            db.Stars.Insert(Name: "Alpha Centauri", Hydrogen: 79.68);
 
            var insertedStars = db.Stars.All();
 
            foreach (var item in insertedStars)
            {
                Console.WriteLine(string.Format("{0}: {1}%", item.Name, item.Hydrogen));
            }
 
            Console.ReadKey();
        }
    }
}

And the result:

I hope that you’ll read more here