August 26, 2013

LINQ to DB. Introduction.

LINQ to DB is a database LINQ provider that allows you working with your database using an application object model.

In addition to basic LINQ query operations, LINQ to DB also introduces DML and DDL extensions such as Insert, Delete, Update, CreateTable, DropTable, etc. methods.

Why LINQ to DB

Comparing to other database LINQ providers, LINQ to DB is not a typical ORM that supports entity services such as automatic object storage, caching, change tracking, object identity, etc. LINQ to DB explicitly translates your LINQ queries into SQL and maps the result to your application object model. Instead of hiding your database from you, LINQ to DB concentrates on high performance, generating clear, readable and human friendly SQL, and close to SQL programming model including DDL and DML operations. In fact, you can consider LINQ to DB as type-safe SQL embedded in C#, VB.NET, and any other .NET languages. LINQ to DB is a straightforward LINQ into SQL translator that avoids any magic, implicity, and unexpected surprises.

Get started

The following example demonstrates how to start using LINQ to DB.

  • Create a project.
  • Add LINQ to DB NuGet into your solution.
  • Use the following code to test the library:
using System;
using System.Linq;

using LinqToDB.DataProvider.SqlServer;

namespace ConsoleApplication1
{
    public class Categories
    {
        public int    CategoryID;
        public string CategoryName;
        public string Description;
        public byte[] Picture;
    }

    class Program
    {
        const string ConnectionString =
            "Data Source=.;Database=Northwind;Integrated Security=SSPI";

        static void Main()
        {
            using (var db = SqlServerTools.CreateDataConnection(ConnectionString))
            {
                var q =
                    from c in db.GetTable<Categories>()
                    select c;

                foreach (var category in q)
                {
                    Console.WriteLine("ID : {0}, Name : {1}",
                        category.CategoryID,
                        category.CategoryName);
                }
            }
        }
    }
}

More advanced example (using app.config configuration and entity mapping attributes):

App.config
<?xml version="1.0"?>
<configuration>
    <connectionStrings>
        <add name="Northwind" 
            connectionString="Data Source=.;Database=Northwind;Integrated Security=SSPI"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>
Program.cs
using System;
using System.Linq;

using LinqToDB.Data;
using LinqToDB.Mapping;

namespace ConsoleApplication1
{
    [Table("Categories")]
    public class Category
    {
        [PrimaryKey, Identity] public int    CategoryID;
        [Column, NotNull]      public string CategoryName;
        [Column,    Nullable]  public string Description;
        [Column,    Nullable]  public byte[] Picture;
    }

    class Program
    {
        static void Main()
        {
            using (var db = new DataConnection())
            {
                var q =
                    from c in db.GetTable<Category>()
                    select c;

                foreach (var category in q)
                {
                    Console.WriteLine("ID : {0}, Name : {1}",
                        category.CategoryID,
                        category.CategoryName);
                }
            }
        }
    }
}

Supported databases

  • DB2
  • Firebird
  • Informix
  • Microsoft Access
  • Microsoft Sql Server
  • Microsoft SqlCe
  • MySql
  • Oracle
  • PostgreSQL
  • SQLite
  • Sybase ASE

Supported frameworks

  • .NET 4.x
  • Silverlight 4+
  • Mono

Links

3 comments:

  1. Online Casino | Bet On Baccarat | Real Money | FABCasino
    Find a list of casinos that allow players to 제왕카지노 play online casino 바카라사이트 games, plus how to download and install the game. Check link 12bet out

    ReplyDelete
  2. M & S fabricates parts and gear enclosures from selection of|quite so much of|a wide range of} alloys; steel, scorching rolled steel, cold rolled steel, stainless steel, aluminum, copper, brass, & Heater nu-metal. In addition, our craftsmen can fully assemble and insulate completed parts, then ship them ready-to-use. The services supplied by metallic fabricators are in high demand in lots of} business, industrial, and shopper markets. Commercially, fabricated metallic parts and merchandise are discovered as and in furnishings, desktop paper trays, shelving, and HVAC components including ductwork and grating.

    ReplyDelete
  3. Should you care about how the other gamers behave if you play roulette online? You can base your recreation on plenty of totally different roulette methods, however have the ability to|you probably can}'t win at roulette every time you play. This is essential as it doubles the advantage for 코인카지노 the casino. On a European wheel you'd anticipate to lose, in the long run|the lengthy term}, 2.7% of any cash you guess with.

    ReplyDelete