XY-ORM tutorial 1: tailism, I want to immediately will use xiangyi platform!

Source: Internet
Author: User

The first thing we hope for is how to use new things. OK. Let's take a look at how to use the most basic entity operations through the xiangyi platform.

Step 1: Download and decompress the version file. Let's see what the new version has:

This is the root directory: Contains 3 files and 1 Folder:

 

Open the dll folder, which is the core dll file (we will only discuss 32-bit, 64-bit operations, just ignore it)

 

Step 2: understand how the project configuration file ProjectConfig. xycfg works

Open the project configuration file and you can see the following:

 

Step 3: Create a table and use the platform to add, delete, modify, and query tables:

3.1 basic conditions:

1) Database Type SqlServer;

2) connection statement: server =.; database = northwind; uid = sa; pwd = 123;

3) The UserInfo table contains the following fields:

Id varchar (60) -- indicates the primary key of the field and the string type;

Createtime datetime -- indicates the Creation Time, time type;

Amt decimal () -- indicates the amount, decimal type;

Qty int -- number, integer type;

Name varchar (30) -- indicates the user name and string type;

Sex varchar (10) -- indicates gender and string type (the enum type corresponds to the object );

IsVip int -- indicates whether to use vip. It is of the bool type when it is an integer)

3.2 configure the connection driver and connection string:

<!-- ConnectionCfg -->
<ConnectionCfgs MainDbName="MainDb" Encrypt="False">
<ConnectionCfg DbName="MainDb" DbType="SqlServer" ConnString="server=.;database=northwind;uid=sa;pwd=123;timeout=15;" />
</ConnectionCfgs>

<!-- ProviderFactoryCfg -->
<ProviderFactoryCfgs>
<ProviderFactoryCfg DbType="SqlServer" ProviderInvariantName="System.Data.SqlClient">
</ProviderFactoryCfg>
</ProviderFactoryCfgs>

3.3 execute the create table statement:

create table UserInfo(id varchar(60) not null primary key,name varchar(30),sex varchar(10),qty int,amt decimal(18,2),isVip int,createtime datetime)

3.4 create an object corresponding to the table:

Note the following: 1. BaseEo must be inherited; 2. Table attribute must be added: TableAttribute; parameter: Table Name; 3. attribute: ColumnAttribute; parameter: column name;

/// <Summary>
/// User entity.
/// </Summary>
/// <Remarks> all objects must inherit BaseEo. Otherwise, the platform functions cannot be used. </remarks>
[Table ("UserInfo")]
Public class UserInfo: BaseEo
{
/// <Summary>
/// ID, indicating the primary key.
/// </Summary>
[Column ("id")]
Public string Id {get; set ;}

/// <Summary>
/// User name.
/// </Summary>
[Column ("name")]
Public string Name {get; set ;}

/// <Summary>
/// Quantity.
/// </Summary>
[Column ("qty")]
Public int Qty {get; set ;}

/// <Summary>
/// Gender.
/// </Summary>
/// <Remarks> the platform supports direct binding of enumeration attributes </remarks>
[Column ("sex")]
Public Sex {get; set ;}

/// <Summary>
/// Whether it is a vip.
/// </Summary>
/// <Remarks> the platform supports direct binding of Boolean attributes. If the database itself does not have a boolean type, you only need to use the integer type, 1 indicates true, 0 indicates false </remarks>
[Column ("isVip")]
Public bool IsVip {get; set ;}

/// <Summary>
/// Creation time.
/// </Summary>
[Column ("createtime")]
Public DateTime CreateTime {get; set ;}

/// <Summary>
/// User amount.
/// </Summary>
[Column ("amt")]
Public decimal Amt {get; set ;}
}

Public enum Sex
{
Male and female
}

3.5 The most impressive thing is coming: Basic addition, deletion, modification, and query

Add, delete, modify, and query

// First, create a user-based Data Access Object:
BaseDao <UserInfo> dao = new BaseDao <UserInfo> ();

// Save a user
UserInfo user = new UserInfo ();
User. Id = "1 ";
User. Name = "James ";
User. Sex = Sex. Male;
User. Qty = 3;
User. Amt = 120.12 M;
User. CreateTime = DateTime. Now;
User. IsVip = true;
Dao. SaveOrUpdate (user );

// Load the saved user
User = dao. load (string. format ("select {0} from {1} where {2} = '{3}'", dao. entity. columnNames, dao. entity. tableName, "id", user. id ));
// Modify the name and update it to the database
User. Name = "Li Si ";
Dao. SaveOrUpdate (user );

// Delete a user
Dao. Delete (user );

I wonder if you have found that the above Code, whether it is saved or updated, is called by SaveOrUpdate. You do not need to manually call Save or Update, because the system will intelligently determine whether the object's persistence status is new or loaded from the database.

Dao. Entity. ColumnNames: the column name;

Dao. Entity. TableName: indicates the table name. You can check the two items during debugging.

Dao. Entity refers to the metadata information of an object. For example:Table Name, Chinese Table Name, column name set (separated by commas), complete column name set (table name + column name, separated by commas). This information is found through reflection. Many people will worry about the reflection speed. In fact, this does not need to worry, because the platform has already done Cache Optimization, so it will only find it once, it is loaded when the project is started (For details, refer:Unified project InitializationTutorial)

 

You can download XySoftDemo1 from here.

Copyright: xiangyi Software

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.