BaseObject ClassNemiro.Data.dll
The base class that implements the object-oriented data access (Object-Relational Mapping, ORM).
Inheritance Hierarchy

System Object
  Nemiro.Data.Sql BaseObject

Namespace: Nemiro.Data.Sql
Assembly: Nemiro.Data (in Nemiro.Data.dll) Version: 2.11.4.126 (2.11.4.126)
Syntax

public abstract class BaseObject

The BaseObject type exposes the following members.

Constructors

  NameDescription
Public methodBaseObject 
Called from constructors in derived classes in order to initialize the BaseObject class.
Public methodBaseObject(DataRow)
Called from constructors in derived classes in order to initialize the BaseObject class.
Public methodBaseObject(Object)
Called from constructors in derived classes in order to initialize the BaseObject class.
Public methodBaseObject(Object, CacheInfo)
Called from constructors in derived classes in order to initialize the BaseObject class.
Public methodBaseObject(Object, Int32)
Called from constructors in derived classes in order to initialize the BaseObject class.
Protected methodBaseObject(DataRow, Object, Int32, CacheInfo)
Called from constructors in derived classes in order to initialize the BaseObject class.
Top
Methods

  NameDescription
Public methodCreateTable
Creates a database table based on the scheme described in the instance of the derived class.
Public methodDelete
Removes an instance of the derived data from the database.
Public methodEquals (Inherited from Object.)
Public methodExists
Checks the existence of data in the table of database.
Protected methodFinalize (Inherited from Object.)
Public methodGetChanges
Returns information about the data changes.
Public methodGetHashCode (Inherited from Object.)
Public methodStatic memberGetList
Returns a collection of instances of the derived class.
Public methodGetType (Inherited from Object.)
Public methodLoad 
Loads to instance of the derived class data by primary key field or unique fields.
Public methodLoad(DataRow)
Loads to instance of the derived class data from DataRow instance.
Public methodLoadJson
Loads data from JSON to instance of the derived class.
Public methodLoadXml(String)
Loads data from the XML-document to instance of the derived class.
Public methodLoadXml(String, Encoding)
Loads data from the XML-document to instance of the derived class with specified encoding.
Protected methodMemberwiseClone (Inherited from Object.)
Public methodSave
Saves data of derived class instance to the database.
Public methodTableExists
Checks an existence the table in the database, described in the instance of the derived class.
Public methodToJson
Serializes the instance of the derived class to JSON.
Public methodToString
Returns an instance of the derived class as a string in the format JSON.
(Overrides Object ToString .)
Public methodToXml 
Serializes an instance of the derived class to XML.
Public methodToXml(Encoding)
Serializes an instance of the derived class to XML with specified encoding.
Public methodToXml(Encoding, Formatting)
Serializes an instance of the derived class to XML with specified encoding and formatting options.
Top
Properties

  NameDescription
Public propertyCache
Cache options.
Public propertyCacheDuration
Duration caching the query results (in seconds). Minus one or zero - without caching (default).
Public propertyConnectionString
Connection string. Default value: LocalSqlServer.
Public propertyRowData
DataRow on which builded an instance of the derived class.
Public propertyTableName
Table name.
Top
Remarks

From this class must inherit all the objects through which it is necessary to interact with SQL Server database.

ORM classes can be created manually or by using the program DB2Class3: http://data.nemiro.net

The default connection string is LocalSqlServer.

<remove name="LocalSqlServer"/> 
<add 
  name="LocalSqlServer" 
  connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=example;Trusted_Connection=True;" 
/>
Examples

The following example illustrates an embodiment in class Users table of the same name.

using System;
using System.Collections.Generic;
using Nemiro.Data;
using Nemiro.Data.Sql;
using System.Data;

[Table("users")]
public class Users : BaseObject
{

  [Column("id_users", SqlDbType.Int, ColumnAttributeFlags.PrimaryKey | ColumnAttributeFlags.Identity)]
  public int IdUsers { get; set; }

  [Column("first_name", SqlDbType.NVarChar, Size = 50)]
  public string FirstName { get; set; }

  [Column("last_name", SqlDbType.NVarChar, Size = 50)]
  public string LastName { get; set; }

  [Column("email", SqlDbType.VarChar, Size = 100)]
  public string Email { get; set; }

  [Column("phone", SqlDbType.VarChar, Size = 30)]
  public string Phone { get; set; }

  [Column("sex", SqlDbType.Char, Size = 1)]
  public string Sex { get; set; }

  [Column("birthday", SqlDbType.DateTime, ColumnAttributeFlags.AllowNull)]
  public DateTime? Birthday { get; set; }

  [Column("date_created", SqlDbType.DateTime, Default = ColumnDefaultValues.Now)]
  public DateTime DateCreated { get; set; }

}

Now that there is a class implementing access to the table users, you can work with the data through this object.

If the table users in the database does not exist, it can be easy to create, as shown in the following example.

// create instance of Users class
Users u = new Users();
// check the existence of a table in the database 
if (!u.TableExists())
{
  // table not found,  
        // create table
  u.CreateTable();
}

Constantly check the existence of tables in the database is not recommended. You can do this once, when you install or run the application.

Work directly with the data provided by three simple methods: Load , Save  and Delete .

The following example shows how add two new users to the users table. Data is added after calling Save .

// create instance of Users class
Users u = new Users();
// specify the user data
u.FirstName = "John";
u.LastName = "Smith";
u.Sex = "M";
u.Email = "j.smith@example.org";
u.Birthday = new DateTime(1980, 1, 1);
// save user to database
u.Save();

// output
Console.WriteLine("User ID: {0}", u.IdUsers);

// create instance of Users class
u = new Users();
// specify the user data
u.FirstName = "Anna";
u.LastName = "Smith";
u.Sex = "F";
u.Email = "a.smith@example.org";
u.Birthday = new DateTime(1985, 10, 15);
// save user to database
u.Save();

// output
Console.WriteLine("User ID: {0}", u.IdUsers);

Each record in the database has a unique identifier. In this example, the primary key is id_users field of table users. In the Users class to indicate that the attributes PrimaryKey and Identity.

// create instance of Users class
Users u = new Users();
// specifies the user identifier
u.IdUsers = 1;
// loading data of user to class instance 
u.Load();

// output
Console.WriteLine("Name:\t\t {0} {1}", u.FirstName, u.LastName);
Console.WriteLine("Gender:\t\t {0}", u.Sex);
Console.WriteLine("Birthday:\t {0}", u.Birthday.Value.ToShortDateString());
Console.WriteLine("Email:\t\t {0}", u.Email);

If the specified identifier, calling Save  will not create a new record, and save the changes to an existing record.

// create instance of Users class
Users u = new Users();
// specifies the user identifier
u.IdUsers = 1;
// loading data of user to class instance 
u.Load();
// the data is loaded or not 
if (u.IdUsers <= 0)
{
  // ID is null, then the data in the database is not found
  Console.WriteLine("User not found...");
  return;
}
// user data successfully loaded 
// output
Console.WriteLine("User ID: {0}", u.IdUsers);
// change user data
u.FirstName = "Max";
u.LastName = "Robinzon";
// save
u.Save();
// output user id
Console.WriteLine("User ID: {0}", u.IdUsers);

Base class BaseObject has overloads that allow you to further simplify data access.

Use the DB2Class3, for automatically create a more comfortable classes for data access.

See Also