Nemiro.Data.Sql BaseObject
Namespace: Nemiro.Data.Sql
Assembly: Nemiro.Data (in Nemiro.Data.dll) Version: 2.11.4.126 (2.11.4.126)
The BaseObject type exposes the following members.
Name | Description | |
---|---|---|
BaseObject |
Called from constructors in derived classes in order to initialize the BaseObject class.
| |
BaseObject(DataRow) |
Called from constructors in derived classes in order to initialize the BaseObject class.
| |
BaseObject(Object) |
Called from constructors in derived classes in order to initialize the BaseObject class.
| |
BaseObject(Object, CacheInfo) |
Called from constructors in derived classes in order to initialize the BaseObject class.
| |
BaseObject(Object, Int32) |
Called from constructors in derived classes in order to initialize the BaseObject class.
| |
BaseObject(DataRow, Object, Int32, CacheInfo) |
Called from constructors in derived classes in order to initialize the BaseObject class.
|
Name | Description | |
---|---|---|
CreateTable |
Creates a database table based on the scheme described in the instance of the derived class.
| |
Delete |
Removes an instance of the derived data from the database.
| |
Equals | (Inherited from Object.) | |
Exists |
Checks the existence of data in the table of database.
| |
Finalize | (Inherited from Object.) | |
GetChanges |
Returns information about the data changes.
| |
GetHashCode | (Inherited from Object.) | |
GetList |
Returns a collection of instances of the derived class.
| |
GetType | (Inherited from Object.) | |
Load |
Loads to instance of the derived class data by primary key field or unique fields.
| |
Load(DataRow) |
Loads to instance of the derived class data from DataRow instance.
| |
LoadJson |
Loads data from JSON to instance of the derived class.
| |
LoadXml(String) |
Loads data from the XML-document to instance of the derived class.
| |
LoadXml(String, Encoding) |
Loads data from the XML-document to instance of the derived class with specified encoding.
| |
MemberwiseClone | (Inherited from Object.) | |
Save |
Saves data of derived class instance to the database.
| |
TableExists |
Checks an existence the table in the database, described in the instance of the derived class.
| |
ToJson |
Serializes the instance of the derived class to JSON.
| |
ToString |
Returns an instance of the derived class as a string in the format JSON.
(Overrides Object ToString .) | |
ToXml |
Serializes an instance of the derived class to XML.
| |
ToXml(Encoding) |
Serializes an instance of the derived class to XML with specified encoding.
| |
ToXml(Encoding, Formatting) |
Serializes an instance of the derived class to XML with specified encoding and formatting options.
|
Name | Description | |
---|---|---|
Cache |
Cache options.
| |
CacheDuration |
Duration caching the query results (in seconds). Minus one or zero - without caching (default).
| |
ConnectionString |
Connection string.
Default value: LocalSqlServer.
| |
RowData |
DataRow on which builded an instance of the derived class.
| |
TableName |
Table name.
|
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;" />
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.