标签:
NetUML.DataEngine 数据引擎支持多数据库,数据访问引擎采用配置方式,类似ibatis.net底层原理,支持多数据库连接方式。将来可支持数据库读写分离,读写分离配置采用MVC路由机制。
源码结构
providers.config 配置文件名称,只需把它放在程序的根目录下即可。
1 <provider name="oracleManagedDataAccess" 2 description="Oracle, Microsoft provider V1.0.5000.0" 3 enabled="true" 4 default="false" 5 assemblyName="Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342"
connectionClass="Oracle.ManagedDataAccess.Client.OracleConnection" 6 commandClass="Oracle.ManagedDataAccess.Client.OracleCommand" 7 parameterClass="Oracle.ManagedDataAccess.Client.OracleParameter" 8 parameterDbTypeClass="Oracle.ManagedDataAccess.Client.OracleDbType" 9 parameterDbTypeProperty="OracleType" 10 dataAdapterClass="Oracle.ManagedDataAccess.Client.OracleDataAdapter" 11 commandBuilderClass="Oracle.ManagedDataAccess.Client.OracleCommandBuilder" 12 usePositionalParameters="false" 13 useParameterPrefixInSql="true" 14 useParameterPrefixInParameter="false" 15 parameterPrefix=":" 16 allowMARS="false" 17 />
assemblyName:访问数据库的DLL程序集信息,此DLL文件也必须放在程序根目录下。
App.Config 配置数据库连接字符串
<add providerName="oracleManagedDataAccess" name="oracle"
connectionString="User ID=d;Password=d;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)))
(CONNECT_DATA=(SERVICE_NAME=ORCL)));"/>
providerName:采用哪个配置信息访问数据库,这里采用oracleManagedDataAccess的配置。
在程序启动的时候调用 NetUML.DataAccess.DbHelper.InitDBConfig();
1、新建表“Class1”
1 /** 创建表[Class1] **/ 2 CREATE TABLE [dbo].[Class1]( 3 [ID] [int] IDENTITY (1, 1) NOT NULL , 4 [aa] [nvarchar](50) NULL , 5 [bb] [datetime] NULL , 6 [cc] [bit] NULL , 7 [ee] [int] NULL , 8 CONSTRAINT [PK_Class1] PRIMARY KEY 9 ( 10 [ID] ASC 11 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 12 ) ON [PRIMARY] 13 GO
2、生成“ClassModel”
1 ///<summary> 2 ///Class1 3 ///<summary> 4 [Table("Class1")] 5 public class ClassModel: NetUML.DataEngine.ActiveEntity 6 { 7 8 /// <summary> 9 /// [ID] 10 /// </summary> 11 [Key(KeyType.Indentity)] 12 public int ID 13 { 14 get;set; 15 } 16 private string _aa; 17 /// <summary> 18 /// [aa] 19 /// </summary> 20 public string aa 21 { 22 set 23 { 24 if (_aa == value) return; 25 OnChanged("aa", _aa, value); 26 _aa = value; 27 } 28 get { return _aa;} 29 } 30 private DateTime? _bb; 31 /// <summary> 32 /// [bb] 33 /// </summary> 34 public DateTime? bb 35 { 36 set 37 { 38 if (_bb == value) return; 39 OnChanged("bb", _bb, value); 40 _bb = value; 41 } 42 get { return _bb;} 43 } 44 private bool? _cc; 45 /// <summary> 46 /// [cc] 47 /// </summary> 48 public bool? cc 49 { 50 set 51 { 52 if (_cc == value) return; 53 OnChanged("cc", _cc, value); 54 _cc = value; 55 } 56 get { return _cc;} 57 } 58 private int? _ee; 59 /// <summary> 60 /// [ee] 61 /// </summary> 62 public int? ee 63 { 64 set 65 { 66 if (_ee == value) return; 67 OnChanged("ee", _ee, value); 68 _ee = value; 69 } 70 get { return _ee;} 71 } 72 }
新建表和生成Model代码都是用DataUML 软件生成的,如下图:
添加、修改、删除数据操作
1 //添加数据 2 ClassModel model = new ClassModel(); 3 model.aa = "ss"; 4 model.bb = DateTime.Now; 5 model.Save(); 6 //修改数据 7 ClassModel model = new ClassModel(); 8 model.ID = 1; 9 model.aa = "bb"; 10 model.Update(); 11 // 删除数据 12 ClassModel model = new ClassModel(); 13 model.ID = 1; 14 model.Delete();
app.config 数据库配置信息如下:
<add providerName="oracleManagedDataAccess" name="oracle" connectionString="User ID=a;Password=a;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.1)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=ORCL)));"/> <add providerName="sqlServer2.0" name="sqlconnect1" connectionString="Data Source=192.168.0.1;Initial Catalog=t;User Id=sa;Password=sa" /> <add providerName="sqlServer2.0" name="sqlconnect2" connectionString="Data Source=192.168.0.1;Initial Catalog=t;User Id=sa;Password=sa" />
Model的信息如下。
1 ///<summary> 2 ///采用“sqlconnect1” 这个数据库连接字条串进行读写数据 3 ///<summary> 4 [Table("Class1", ConnectionName = "sqlconnect1")] 5 public class ClassModel: NetUML.DataEngine.ActiveEntity 6 { 7 [Key(KeyType.Indentity)] 8 public int ID 9 { 10 get;set; 11 } 12 }
当读写数据的时候,会采用
<add providerName="sqlServer2.0" name="sqlconnect1" connectionString="Data Source=192.168.0.1;Initial Catalog=t;User Id=sa;Password=sa" />
这条数据库连接字符串。
1、增加读写分离配置、多数据库操作配置,无需在类上面定义ConnectionName属性,采用配置方式进行设置
2、增加面向对象的概念,类与类之间映射关系,以及级联操作
3、增加更丰富的查询操作
4、。。。。。。。。。。
源码下载
标签:
原文地址:http://www.cnblogs.com/netuml/p/4648498.html