1. Add reference to libraries

To access database, RB.DataAccess.dll must be added to the reference of your project first. However, the DLL which implements the access to a specific database must be added too. We have a default implementation in RB.DataAccess.SqlServer.dll for Microsoft SQL Server. So, if you are going to use it to access MS SQL Server (or Express), you should add it to your project reference.

2. Configure in the Web.Config

The database access libraries rely on the configuration in the Web.Config. To specify the concrete library and the connection we are going to use, we must configure a "RB.DataAccess" section.
First, we add a section definition to "configSections" in the Web.Config.
    <section name="RB.DataAccess" type="RB.DataAccess.Configuration.DataAccessConfigSection, RB.DataAccess, Version=, Culture=neutral, PublicKeyToken=984bd00ede0db319" />

Second, we add the "RB.DataAcess" section. The section has one property "defaultContext", which specify the default context we are going to use. Under the section, we must add a collection of contexts, which defined by a “contexts" tag. In the "contexts" tag, we can add context definitions.
A context element 3 properties,
  • name the name of the context, which is a identifier of a context in the program
  • type the type of data context class related to a specific database
  • connectionStringName the name of connection string element defined in the "ConnectionStrings" section.

<RB.DataAccess defaultContext="defaultContext">
      <add name="defaultContext" type="RB.DataAccess.SqlServer.MsSqlDataContext, RB.DataAccess.SqlServer, Version=, Culture=neutral, PublicKeyToken=9470e63a380323ef" connectionStringName="localTestServer" />

3. Query in the default context

First, you must create a database context. And then, an executor must be created. An executor is a handler to execute any SQL operations. We can create a queryable object from it, so that we can use linq syntax on it and then execute it.
var q = from p in DataContext.CreateExecutor().Query<TItem>()
            where p.Id == 1
            select p;
var item = q.First();

Last edited May 31, 2012 at 4:03 AM by robinz1001, version 9


No comments yet.