// ToEntity1st(查询数据,并将首条数据转换为数据实例)
using JCoder.Db4Net;
using JCoder.Db4Net.ORM;
IDbOperator _operator = new MsSqlFactoryOperator()
{
ConnectionString = "Server=127.0.0.1;Database=test;Uid = user; Pwd=testpassword;"
};
return this.Operator.ToEntity1st("select top 1 * from table1;");
1
2
3
4
5
6
7
8
9
2)ToEntities(查询数据,并转换为数据实体列表)
// ToEntities(查询数据,并转换为数据实体列表)
using JCoder.Db4Net;
using JCoder.Db4Net.ORM;
IDbOperator _operator = new MsSqlFactoryOperator()
{
ConnectionString = "Server=127.0.0.1;Database=test;Uid = user; Pwd=testpassword;"
};
return this.Operator.ToEntities("select top 10 * from table1;");
1
2
3
4
5
6
7
8
9
3)CRUD(增删查改)
using JCoder.Db4Net;
using JCoder.Db4Net.ORM;
// Test Entity Class
[DbTable("t_table_test")]
public class TestEntity
{
[DbPrimaryKey]
[DbAutoIncrement]
public int Key001 { get; set; }
[DbPrimaryKey]
[DbField(IsUnique=true)]
public string Key002 { get; set; }
[DbIndex("idx01")]
public string C003 { get; set; }
}
IDbOperator _operator = new MsSqlFactoryOperator()
{
ConnectionString = "Server=127.0.0.1;Database=test;Uid = user; Pwd=testpassword;"
};
TestEntity x = new TestEntity()
{
Key001 = 101,
Key002 = "Test PK",
};
// Insert Data(新增数据)
this.Operator.Insert(x);
// Update Data(更新数据)
x.C003 = "Update Test";
this.Operator.Update(x);
// Delete Data(删除数据)
this.Operator.Delete(x);
// Select Data(查询数据)
this.Operator.Select1st(" and Key001=101 and Key002-'Test PK'");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
4)Create\Drop Table From Entity(由实体类创建\删除数据库的数据表)
using JCoder.Db4Net;
using JCoder.Db4Net.ORM;
// Test Entity Class
[DbTable("t_table_test")]
public class TestEntity
{
[DbPrimaryKey]
[DbAutoIncrement]
public int Key001 { get; set; }
[DbPrimaryKey]
[DbField(IsUnique =true)]
public string Key002 { get; set; }
[DbIgnore]
public string Key003 { get; set; }
[DbIndex]
[DbField(AllowNull = false, Default = "", Description = "Test Comment")]
public string C001 { get; set; }
[DbIndex("idx01")]
public string C002 { get; set; }
[DbIndex("idx01")]
public string C003 { get; set; }
[DbIgnore]
public int C004 { get; set; }
[DbField(AllowNull = false, Default = "0", Description = "Test Comment2")]
public int C005 { get; set; }
[DbNumberFieldExt(Precision = 11, Scale = 2)]
public double C006 { get; set; }
[DbField(AllowNull = false, Default = "", Description = "Test Comment2")]
public string C007 { get; set; }
public string C008 { get; set; }
[DbTimeFieldExt(UpdateTimeOnCreate = true, UpdateTimeOnUpdate = true)]
public DateTime C009 { get; set; }
}
IDbOperator _operator = new MsSqlFactoryOperator()
{
ConnectionString = "Server=127.0.0.1;Database=test;Uid = user; Pwd=testpassword;"
};
this.Operator.CreateTable();
this.Operator.DropTable();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
3.5 Orm工具类使用
1)转换首条数据为实体对象(支持DbDataReader、DataTable)
using JCoder.Db4Net.ORM;
// data source
var _table = new DataTable();
// Property only.
var _result = JCoder.Db4Net.ORM.Orm.ToObject1st(_table);
var _result = _table.ToObject1st();
// Attributes first
var _result = JCoder.Db4Net.ORM.Orm.ToEntity1st(_table);
var _result = _table.ToEntity1st();
1
2
3
4
5
6
7
8
9
10
11
2)将数据转换为实体对象(支持DataRow、IDictionary、IDictionary))
using JCoder.Db4Net.ORM;
// data source
var _dict = new Dictionary();
// Attributes first
var _result = JCoder.Db4Net.ORM.Orm.ToEntity(_dict);
var _result = _dict.ToEntity();
// Property only.
var _result = JCoder.Db4Net.ORM.Orm.ToObject(_dict);
var _result = _dict.ToObject();
1
2
3
4
5
6
7
8
9
10
11
3)将数据源转换为实体对象列表(支持DbDataReader、DataTable)
using JCoder.Db4Net.ORM;
// data source
var _table = new DataTable();
// Attributes first
var _result = JCoder.Db4Net.ORM.Orm.ToEntities(_table);
var _result = _table.ToEntities();
// Property only.
var _result = JCoder.Db4Net.ORM.Orm.ToObjects(_table);
var _result = _table.ToObjects();
1
2
3
4
5
6
7
8
9
10
11
4)将数据源转换为实体对象字典(支持DbDataReader、DataTable)
using JCoder.Db4Net.ORM;
// data source
var _table = new DataTable();
_table.Columns.Add(new DataColumn("ID", typeof(string)));
// Property only.
var _result = JCoder.Db4Net.ORM.Orm.ToObjectDict(_table, "ID");
var _result = _table.ToObjectDict("ID");
// Attributes first
var _result = JCoder.Db4Net.ORM.Orm.ToEntityDict(_table, "ID");
var _result = _table.ToEntityDict("ID");
using JCoder.Db4Net.ORM;
var _target = new T();
// data source
var _source = new DbDataReader();
// Property only.
var _result = JCoder.Db4Net.ORM.Orm.FillInObject(_source, _target);
var _result = _source.FillInObject(_target);
// Attributes first
var _result = JCoder.Db4Net.ORM.Orm.FillInEntity(_source, _target);
var _result = _source.FillInEntity(_target);
1
2
3
4
5
6
7
8
9
10
11
12
13
6)将实体对象转换为DataTable
using JCoder.Db4Net.ORM;
var _list = new List();
var t = new T();
// Property only.
// 实体对象转换为DataTable
DataTable _table = JCoder.Db4Net.ORM.Orm.ObjectToDataTable(t);
DataTable _table = t.ObjectToDataTable();
// 实体对象列表转换为DataTable
DataTable _table = JCoder.Db4Net.ORM.Orm.ObjectsToDataTable(_list);
DataTable _table = _list.ObjectsToDataTable();
// Attributes first
// 实体对象转换为DataTable
DataTable _table = JCoder.Db4Net.ORM.Orm.EntityToDataTable(t);
DataTable _table = t.EntityToDataTable();
// 实体对象列表转换为DataTable
DataTable _table = JCoder.Db4Net.ORM.Orm.EntitiesToDataTable(_list);
DataTable _table = _list.EntitiesToDataTable();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
7)将List转换为DataTable
using JCoder.Db4Net.ORM;
// 将List转换为DataTable(使用特性进行排序,自定义名称等)
public class BoTest
{
[DbSubField("Width", "S001-Width", Sort = 2120)]//获取Size对象里的Width属性值,并显示为"S001-Width"
[DbSubField("Height", "S001-Height", Sort = 2121)]//获取Size对象里的Height属性值,并显示为"S001-Height"
public Size S001 { get; set; }
public string Key { get; set; }
[DbEnumName]//枚举显示为文本
public AttributeTargets AT { get; set; }
public AttributeTargets AT2 { get; set; }
[DbSort(100)]
public string P100 { get; set; }
[DbSort(111)]//设置排序值(从小到大依次排序)
public string P111 { get; set; }
public DateTime CreateTime { get; set; }
[DbIgnoreConvertion]//转换时忽略
public int Z001 { get; set; }
}
var _list = new List();
for (int i = 0; i < 100; i++)
{
_list.Add(new()
{
S001 = new(i * 100, i * 1000),
Key = $"Key_{i}",
P100 = $"P100_{i}",
P111 = $"P111_{i}",
CreateTime = DateTime.Now,
Z001 = i,
AT = AttributeTargets.Interface,
AT2 = AttributeTargets.Delegate,
});
}
var _table = JCoder.Db4Net.ORM.Orm.EntitiesToDataTable(_list);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
8)将实体对象转换为Dictionary
using JCoder.Db4Net.ORM;
var t = new T();
// Property only.
// 实体对象转换为Dictionary
Dictionary _dict = JCoder.Db4Net.ORM.Orm.ObjectToDict(t);
Dictionary _dict = t.ObjectToDict();
// Attributes first
// 实体对象转换为Dictionary
Dictionary _dict = JCoder.Db4Net.ORM.Orm.EntityToDict(t);
Dictionary _dict = t.EntityToDict();