- 下载代码生成器(库也在代码生成器的根目录中) (顺便说说MOON相对其他ORM的优势: )
- 找到配置文件进行配置(实际项目中也一样).
- 配置默认的系统数据库类型(
各类数据库的配置)
MYSQL的配置
http://www.mysql.com/downloads/mirror.php?id=406542 mysql 2.0下载地址
Sqlite的配置PostgreSql的配置
MSSQL的配置
- 给大家一个全配置,到时候好引用,记住只要其中一种类型
<appSettings>
<!--SQLITE-->
<add key="Password" value="sqlite的密码,只有sqlite设置了密码时才需要此节点"/>
<add key="dbType" value="SQLITE" />
<add key="linkString" value="c:/a.db" />
<!-- MSSQL-->
<add key="dbType" value="MSSQL" />
<add key="linkString" value="Server=www.eee.com.cn;Database=hd01systemdb_new;uid=db;Password=mingyue1;" />
<!--MYSQL-->
<add key="dbType" value="MYSQL" />
<add key="MYSQL_PATH" value="c:\MySql.Data.dll" />
<add key="linkString" value="Server=localhost;Database=MoonDB;Uid=root;Pwd=root;" />
<!--PostgreSql-->
<add key="dbType" value="PostgreSql" />
<add key="linkString" value="Server=localhost;Database=MoonDB;User ID=postgres;Password=mingyue;" />
</appSettings> - 运行代码生成器,以MYSQL为例.(注意此架构必须要有主键.且唯一的主键,非符合主键)
- 给大家看看数据库结构
-
项目配置文件<add key="dbType" value="MYSQL" />用代码生成器生成实体层. 加入你要做的项目中.(可以把这段代码复制到项目中,也可以编译引用)
<add key="MYSQL_PATH" value="c:\MySql.Data.dll" />
<add key="linkString" value="Server=localhost;Database=MoonDB;Uid=root;Pwd=root;;Charset=gb2312;" />using System;
using Moon.Orm;
using System.Collections.Generic;
namespace MoonDB{
public class recordset:EntityBase{
public recordset():base(){
SetEntityName("`recordset`");
SetFieldsCount(3);
SetPrimaryField(recordsetTable.RecordID);
SetIsSystemSetPrimaryKey(true);
}
private Int64 _RecordID;
public Int64 RecordID{
get{return _RecordID;}
set{
_RecordID=value;
if (this.FieldsChangedDictionary.ContainsKey("`RecordID`")==false)
{this.FieldsChangedDictionary.Add("`RecordID`",value);}
}
}
private Int64 _UserID;
public Int64 UserID{
get{return _UserID;}
set{
_UserID=value;
if (this.FieldsChangedDictionary.ContainsKey("`UserID`")==false)
{this.FieldsChangedDictionary.Add("`UserID`",value);}
}
}
private String _Sumary;
public String Sumary{
get{return _Sumary;}
set{
_Sumary=value;
if (this.FieldsChangedDictionary.ContainsKey("`Sumary`")==false)
{this.FieldsChangedDictionary.Add("`Sumary`",value);}
}
}
}
public class recordsetTable{
public static readonly Field RecordID=new Field("`RecordID`",true);
public static readonly Field UserID=new Field("`UserID`",false);
public static readonly Field Sumary=new Field("`Sumary`",false);
static recordsetTable(){
RecordID.TableName="`recordset`";
UserID.TableName="`recordset`";
Sumary.TableName="`recordset`";
}
}
public class userset:EntityBase{
public userset():base(){
SetEntityName("`userset`");
SetFieldsCount(4);
SetPrimaryField(usersetTable.ID);
SetIsSystemSetPrimaryKey(true);
}
private String _UserName;
public String UserName{
get{return _UserName;}
set{
_UserName=value;
if (this.FieldsChangedDictionary.ContainsKey("`UserName`")==false)
{this.FieldsChangedDictionary.Add("`UserName`",value);}
}
}
private Int32 _Age;
public Int32 Age{
get{return _Age;}
set{
_Age=value;
if (this.FieldsChangedDictionary.ContainsKey("`Age`")==false)
{this.FieldsChangedDictionary.Add("`Age`",value);}
}
}
private Int64 _ID;
public Int64 ID{
get{return _ID;}
set{
_ID=value;
if (this.FieldsChangedDictionary.ContainsKey("`ID`")==false)
{this.FieldsChangedDictionary.Add("`ID`",value);}
}
}
private String _Address;
public String Address{
get{return _Address;}
set{
_Address=value;
if (this.FieldsChangedDictionary.ContainsKey("`Address`")==false)
{this.FieldsChangedDictionary.Add("`Address`",value);}
}
}
}
public class usersetTable{
public static readonly Field UserName=new Field("`UserName`",false);
public static readonly Field Age=new Field("`Age`",false);
public static readonly Field ID=new Field("`ID`",true);
public static readonly Field Address=new Field("`Address`",false);
static usersetTable(){
UserName.TableName="`userset`";
Age.TableName="`userset`";
ID.TableName="`userset`";
Address.TableName="`userset`";
}
}
}实际项目
using System;
using System.Collections.Generic;
using System.Data;
using MoonDB;
using Moon.Orm;
namespace Test
{
class Program
{
public static void Main(string[] args)
{
userset user=new userset();
user.Age=12;
user.UserName="qsmy12345"+DateTime.Now.Ticks;
user.Address=user.UserName;
object newID=DBFactory.Add(user);
Console.WriteLine();
Console.WriteLine(user.GetOnlyMark());
user.Address=DateTime.Now+"a秦仕川a";
DBFactory.Update(user);
string name=DBFactory.GetOneField<string>(usersetTable.Address,usersetTable.ID.Equal(newID));
List<userset> list=DBFactory.GetEntities<userset>(usersetTable.ID.BiggerThan(0));
Console.WriteLine(list[0].Address);
Console.Read();
}
}
}MYSQL.sql文件,自己还原下载
View Code
SET @saved_cs_client = @@character_set_client;
SET character_set_client = utf8;
/*!50001 CREATE TABLE `new_view` (
`id` bigint(20),
`username` varchar(50)
) ENGINE=MyISAM */;
SET character_set_client = @saved_cs_client;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `recordset` (
`RecordID` bigint(20) NOT NULL AUTO_INCREMENT,
`UserID` bigint(20) NOT NULL,
`Sumary` varchar(1000) NOT NULL,
PRIMARY KEY (`RecordID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `userset` (
`UserName` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`Age` int(11) DEFAULT NULL,
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`Address` varchar(1000) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=72 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
INSERT INTO `userset` VALUES ('qsmy其实',12,1,NULL),('qsmy其实11',12,2,NULL),('qsmy其实11',12,3,NULL),('qsmy其实11',12,4,NULL),('qsmy其实11',12,5,NULL),('qsmy其实11',12,6,NULL),('qsmy其实11',12,7,NULL),('qsmy其实11',12,8,NULL),('qsmy其实11',12,9,NULL),('qsmy其实11',12,10,NULL),('qsmy其实11',12,11,NULL),('qsmy其实11',12,12,NULL),('qsmy其实11',12,13,NULL),('qsmy其实11',12,14,NULL),('qsmy其实11',12,15,NULL),('qsmy其实11',12,16,NULL),('qsmy其实11',12,17,NULL),('qsmy其实11',12,18,NULL),('qsmy其实11',12,19,NULL),('qsmy其实11',12,20,NULL),('qsmy其实11',12,21,NULL),('qsmy其实11',12,22,NULL),('qsmy其实11',12,23,NULL),('dd',123,24,NULL),('dd',123,25,NULL),('dd',123,26,NULL),('dd',123,27,NULL),('dd',123,28,NULL),('dd',123,29,NULL),('dd',123,30,NULL),('dd',123,31,NULL),('dd',123,32,NULL),('dd',123,33,NULL),('dd',123,34,NULL),('dd',123,35,NULL),('dd',123,36,NULL),('dd',123,37,NULL),('dd',123,38,NULL),('dd',123,39,NULL),('dd',123,40,NULL),('dd',123,41,NULL),('dd',123,42,NULL),('dd',123,43,NULL),('dd',123,44,NULL),('dd',123,45,NULL),('dd',123,46,NULL),('aaa',11,47,NULL),('aaa',11,48,NULL),('aaa',11,49,NULL),('aaa',11,50,NULL),('aaa',11,51,NULL),('aaa',11,52,NULL),('aaa',11,53,NULL),('aaa',11,54,NULL),('qsmy12345',12,55,NULL),('qsmy12345634694104416406250',12,56,NULL),('qsmy12345634694105064218750',12,57,NULL),('2012-4-7 15:49:36???',12,58,NULL),('2012-4-7 15:52:45???',12,59,NULL),('2012-4-7 15:54:34???',12,60,NULL),('2012-4-7 15:55:01???',12,61,NULL),('2012-4-7 15:56:44???',12,62,NULL),('2012-4-7 15:57:56a???a',12,63,NULL),('2012-4-7 16:00:46a???a',12,64,'qsmy12345634694112457343750'),('qsmy12345634694113184531250',12,65,'2012-4-7 16:01:59a???a'),('qsmy12345634694114936718750',12,66,'2012-4-7 16:04:54a秦仕川a'),('qsmy12345634694115219687500',12,67,'2012-4-7 16:05:22a秦仕川a'),('qsmy12345634694115920781250',12,68,'2012-4-7 16:06:32a秦仕川a'),('qsmy12345634694116243125000',12,69,'2012-4-7 16:07:05a秦仕川a'),('qsmy12345634694116477187500',12,70,'2012-4-7 16:07:28a秦仕川a'),('qsmy12345634694117010468750',12,71,'2012-4-7 16:08:21a秦仕川a');
/*!50001 DROP TABLE IF EXISTS `new_view`*/;
/*!50001 SET @saved_cs_client = @@character_set_client */;
/*!50001 SET @saved_cs_results = @@character_set_results */;
/*!50001 SET @saved_col_connection = @@collation_connection */;
/*!50001 SET character_set_client = utf8 */;
/*!50001 SET character_set_results = utf8 */;
/*!50001 SET collation_connection = utf8_general_ci */;
/*!50001 CREATE ALGORITHM=UNDEFINED */
/*!50013 DEFINER=`root`@`localhost` SQL SECURITY DEFINER */
/*!50001 VIEW `new_view` AS select `userset`.`ID` AS `id`,`userset`.`UserName` AS `username` from `userset` */;
/*!50001 SET character_set_client = @saved_cs_client */;
/*!50001 SET character_set_results = @saved_cs_results */;
/*!50001 SET collation_connection = @saved_col_connection */;using System;
using Moon.Orm;
using System.Collections.Generic;
namespace MoonDB{
public class recordset:EntityBase{
public recordset():base(){
SetEntityName("`recordset`");
SetFieldsCount(3);
SetPrimaryField(recordsetTable.RecordID);
SetIsSystemSetPrimaryKey(true);
}
private Int64 _RecordID;
public Int64 RecordID{
get{return _RecordID;}
set{
_RecordID=value;
if (this.FieldsChangedDictionary.ContainsKey("`RecordID`")==false)
{this.FieldsChangedDictionary.Add("`RecordID`",value);}
}
}
private Int64 _UserID;
public Int64 UserID{
get{return _UserID;}
set{
_UserID=value;
if (this.FieldsChangedDictionary.ContainsKey("`UserID`")==false)
{this.FieldsChangedDictionary.Add("`UserID`",value);}
}
}
private String _Sumary;
public String Sumary{
get{return _Sumary;}
set{
_Sumary=value;
if (this.FieldsChangedDictionary.ContainsKey("`Sumary`")==false)
{this.FieldsChangedDictionary.Add("`Sumary`",value);}
}
}
}
public class recordsetTable{
public static readonly Field RecordID=new Field("`RecordID`",true);
public static readonly Field UserID=new Field("`UserID`",false);
public static readonly Field Sumary=new Field("`Sumary`",false);
static recordsetTable(){
RecordID.TableName="`recordset`";
UserID.TableName="`recordset`";
Sumary.TableName="`recordset`";
}
}
public class userset:EntityBase{
public userset():base(){
SetEntityName("`userset`");
SetFieldsCount(4);
SetPrimaryField(usersetTable.ID);
SetIsSystemSetPrimaryKey(true);
}
private String _UserName;
public String UserName{
get{return _UserName;}
set{
_UserName=value;
if (this.FieldsChangedDictionary.ContainsKey("`UserName`")==false)
{this.FieldsChangedDictionary.Add("`UserName`",value);}
}
}
private Int32 _Age;
public Int32 Age{
get{return _Age;}
set{
_Age=value;
if (this.FieldsChangedDictionary.ContainsKey("`Age`")==false)
{this.FieldsChangedDictionary.Add("`Age`",value);}
}
}
private Int64 _ID;
public Int64 ID{
get{return _ID;}
set{
_ID=value;
if (this.FieldsChangedDictionary.ContainsKey("`ID`")==false)
{this.FieldsChangedDictionary.Add("`ID`",value);}
}
}
private String _Address;
public String Address{
get{return _Address;}
set{
_Address=value;
if (this.FieldsChangedDictionary.ContainsKey("`Address`")==false)
{this.FieldsChangedDictionary.Add("`Address`",value);}
}
}
}
public class usersetTable{
public static readonly Field UserName=new Field("`UserName`",false);
public static readonly Field Age=new Field("`Age`",false);
public static readonly Field ID=new Field("`ID`",true);
public static readonly Field Address=new Field("`Address`",false);
static usersetTable(){
UserName.TableName="`userset`";
Age.TableName="`userset`";
ID.TableName="`userset`";
Address.TableName="`userset`";
}
}
}实际项目
using System;
using System.Collections.Generic;
using System.Data;
using MoonDB;
using Moon.Orm;
namespace Test
{
class Program
{
public static void Main(string[] args)
{
userset user=new userset();
user.Age=12;
user.UserName="qsmy12345"+DateTime.Now.Ticks;
user.Address=user.UserName;
object newID=DBFactory.Add(user);
Console.WriteLine();
Console.WriteLine(user.GetOnlyMark());
user.Address=DateTime.Now+"a秦仕川a";
DBFactory.Update(user);
string name=DBFactory.GetOneField<string>(usersetTable.Address,usersetTable.ID.Equal(newID));
List<userset> list=DBFactory.GetEntities<userset>(usersetTable.ID.BiggerThan(0));
Console.WriteLine(list[0].Address);
Console.Read();
}
}
}MYSQL.sql文件,自己还原下载
View Code
SET @saved_cs_client = @@character_set_client;
SET character_set_client = utf8;
/*!50001 CREATE TABLE `new_view` (
`id` bigint(20),
`username` varchar(50)
) ENGINE=MyISAM */;
SET character_set_client = @saved_cs_client;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `recordset` (
`RecordID` bigint(20) NOT NULL AUTO_INCREMENT,
`UserID` bigint(20) NOT NULL,
`Sumary` varchar(1000) NOT NULL,
PRIMARY KEY (`RecordID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `userset` (
`UserName` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`Age` int(11) DEFAULT NULL,
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`Address` varchar(1000) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=72 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
INSERT INTO `userset` VALUES ('qsmy其实',12,1,NULL),('qsmy其实11',12,2,NULL),('qsmy其实11',12,3,NULL),('qsmy其实11',12,4,NULL),('qsmy其实11',12,5,NULL),('qsmy其实11',12,6,NULL),('qsmy其实11',12,7,NULL),('qsmy其实11',12,8,NULL),('qsmy其实11',12,9,NULL),('qsmy其实11',12,10,NULL),('qsmy其实11',12,11,NULL),('qsmy其实11',12,12,NULL),('qsmy其实11',12,13,NULL),('qsmy其实11',12,14,NULL),('qsmy其实11',12,15,NULL),('qsmy其实11',12,16,NULL),('qsmy其实11',12,17,NULL),('qsmy其实11',12,18,NULL),('qsmy其实11',12,19,NULL),('qsmy其实11',12,20,NULL),('qsmy其实11',12,21,NULL),('qsmy其实11',12,22,NULL),('qsmy其实11',12,23,NULL),('dd',123,24,NULL),('dd',123,25,NULL),('dd',123,26,NULL),('dd',123,27,NULL),('dd',123,28,NULL),('dd',123,29,NULL),('dd',123,30,NULL),('dd',123,31,NULL),('dd',123,32,NULL),('dd',123,33,NULL),('dd',123,34,NULL),('dd',123,35,NULL),('dd',123,36,NULL),('dd',123,37,NULL),('dd',123,38,NULL),('dd',123,39,NULL),('dd',123,40,NULL),('dd',123,41,NULL),('dd',123,42,NULL),('dd',123,43,NULL),('dd',123,44,NULL),('dd',123,45,NULL),('dd',123,46,NULL),('aaa',11,47,NULL),('aaa',11,48,NULL),('aaa',11,49,NULL),('aaa',11,50,NULL),('aaa',11,51,NULL),('aaa',11,52,NULL),('aaa',11,53,NULL),('aaa',11,54,NULL),('qsmy12345',12,55,NULL),('qsmy12345634694104416406250',12,56,NULL),('qsmy12345634694105064218750',12,57,NULL),('2012-4-7 15:49:36???',12,58,NULL),('2012-4-7 15:52:45???',12,59,NULL),('2012-4-7 15:54:34???',12,60,NULL),('2012-4-7 15:55:01???',12,61,NULL),('2012-4-7 15:56:44???',12,62,NULL),('2012-4-7 15:57:56a???a',12,63,NULL),('2012-4-7 16:00:46a???a',12,64,'qsmy12345634694112457343750'),('qsmy12345634694113184531250',12,65,'2012-4-7 16:01:59a???a'),('qsmy12345634694114936718750',12,66,'2012-4-7 16:04:54a秦仕川a'),('qsmy12345634694115219687500',12,67,'2012-4-7 16:05:22a秦仕川a'),('qsmy12345634694115920781250',12,68,'2012-4-7 16:06:32a秦仕川a'),('qsmy12345634694116243125000',12,69,'2012-4-7 16:07:05a秦仕川a'),('qsmy12345634694116477187500',12,70,'2012-4-7 16:07:28a秦仕川a'),('qsmy12345634694117010468750',12,71,'2012-4-7 16:08:21a秦仕川a');
/*!50001 DROP TABLE IF EXISTS `new_view`*/;
/*!50001 SET @saved_cs_client = @@character_set_client */;
/*!50001 SET @saved_cs_results = @@character_set_results */;
/*!50001 SET @saved_col_connection = @@collation_connection */;
/*!50001 SET character_set_client = utf8 */;
/*!50001 SET character_set_results = utf8 */;
/*!50001 SET collation_connection = utf8_general_ci */;
/*!50001 CREATE ALGORITHM=UNDEFINED */
/*!50013 DEFINER=`root`@`localhost` SQL SECURITY DEFINER */
/*!50001 VIEW `new_view` AS select `userset`.`ID` AS `id`,`userset`.`UserName` AS `username` from `userset` */;
/*!50001 SET character_set_client = @saved_cs_client */;
/*!50001 SET character_set_results = @saved_cs_results */;
/*!50001 SET collation_connection = @saved_col_connection */;