1.一个基本的示例
通过该示例可以撑握以下内容:
1) Gentle.net是怎么把对象(类)和关系数据库(SQL)关联起来的.
2) 使用Gentle.net插入,更新,返回实例信息,和删除数据.
本示例中以人为实例,包括人ID和姓名.附上建表脚本
示例表代码
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->ifexists(select*fromdbo.sysobjectswhereid=object_id(N'[dbo].[Peoples]')andOBJECTPROPERTY(id,N'IsUserTable')=1)
droptable[dbo].[Peoples]
GO
CREATETABLE[dbo].[Peoples](
[PeopleID][int]IDENTITY(1,1)NOTNULL,
[PeopleName][char](10)COLLATEChinese_PRC_CI_ASNOTNULL
)ON[PRIMARY]
GO
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->ifexists(select*fromdbo.sysobjectswhereid=object_id(N'[dbo].[Peoples]')andOBJECTPROPERTY(id,N'IsUserTable')=1)
droptable[dbo].[Peoples]
GO
CREATETABLE[dbo].[Peoples](
[PeopleID][int]IDENTITY(1,1)NOTNULL,
[PeopleName][char](10)COLLATEChinese_PRC_CI_ASNOTNULL
)ON[PRIMARY]
GO
接着附上代码,相关使用请看代码。
<!-- google_ad_client = "pub-6628499292856412"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "text_image"; google_ad_channel = ""; google_color_border = "000000"; google_color_bg = "FFFFFF"; google_color_link = "191919"; google_color_text = "000000"; google_color_url = "008000"; //-->
示例代码
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->usingSystem;
usingGentle.Framework;
namespaceConGentleStudy
{
/**////<summary>
///BaseExample的摘要说明。
///</summary>
publicclassBaseExample
{
[STAThread]
staticvoidMain(string[]args)
{
Console.WriteLine("创建人实例。");
Peoplespeople=newPeoples("xuanfeng");
Console.WriteLine("创建人实例。");
Broker.Insert(people);
Console.WriteLine("创建人实例。");
people.PeopleName="peizunyou";
Console.WriteLine("更新人的信息。");
Broker.Update(people);
Console.WriteLine("创建返回实例信息的Key!");
Keykey=newKey("Peoples",typeof(Peoples),true,"PeopleName","peizunyou");
Console.WriteLine(key.Count.ToString());
Console.WriteLine("创建人实例!");
Peoplespeople1=newPeoples();//必须先实例化申请到空间,下步才不会出错。
people1.PeopleName="xiamfe";
Console.WriteLine("返回一个人实例的信息!");
//Peoplespeople1=(Peoples)Broker.RetrieveInstance(typeof(Peoples),key),这样是将出错
people1=(Peoples)Broker.RetrieveInstance(typeof(Peoples),key);//返回一个实例信息。
Console.WriteLine("输出人的姓名");
Console.WriteLine(people1.PeopleName);
Console.WriteLine("删除信息!");
Keykey_rem=newKey(typeof(Peoples),true,"PeopleID",people1.PeopleID);
Broker.Remove(typeof(Peoples),key_rem);//主意,Key中提供的字段PeopleID是表中的主键,删除才有效!
Console.WriteLine("演示完成,按任何键退出!");
Console.Read();
}
}
[TableName("Peoples")]
publicclassPeoples
{
stringname;
intid;
publicPeoples(int_id,string_name)
{
this.name=_name;
this.id=_id;
}
publicPeoples(string_name)
{
this.name=_name;
}
publicPeoples()
{
}
[TableColumn("PeopleID",true),PrimaryKey(AutoGenerated=true)]
publicintPeopleID
{
get{returnthis.id;}
set{this.id=value;}
}
[TableColumn("PeopleName",true)]
publicstringPeopleName
{
get{returnthis.name;}
set{this.name=value;}
}
}
}
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->usingSystem;
usingGentle.Framework;
namespaceConGentleStudy
{
/**////<summary>
///BaseExample的摘要说明。
///</summary>
publicclassBaseExample
{
[STAThread]
staticvoidMain(string[]args)
{
Console.WriteLine("创建人实例。");
Peoplespeople=newPeoples("xuanfeng");
Console.WriteLine("创建人实例。");
Broker.Insert(people);
Console.WriteLine("创建人实例。");
people.PeopleName="peizunyou";
Console.WriteLine("更新人的信息。");
Broker.Update(people);
Console.WriteLine("创建返回实例信息的Key!");
Keykey=newKey("Peoples",typeof(Peoples),true,"PeopleName","peizunyou");
Console.WriteLine(key.Count.ToString());
Console.WriteLine("创建人实例!");
Peoplespeople1=newPeoples();//必须先实例化申请到空间,下步才不会出错。
people1.PeopleName="xiamfe";
Console.WriteLine("返回一个人实例的信息!");
//Peoplespeople1=(Peoples)Broker.RetrieveInstance(typeof(Peoples),key),这样是将出错
people1=(Peoples)Broker.RetrieveInstance(typeof(Peoples),key);//返回一个实例信息。
Console.WriteLine("输出人的姓名");
Console.WriteLine(people1.PeopleName);
Console.WriteLine("删除信息!");
Keykey_rem=newKey(typeof(Peoples),true,"PeopleID",people1.PeopleID);
Broker.Remove(typeof(Peoples),key_rem);//主意,Key中提供的字段PeopleID是表中的主键,删除才有效!
Console.WriteLine("演示完成,按任何键退出!");
Console.Read();
}
}
[TableName("Peoples")]
publicclassPeoples
{
stringname;
intid;
publicPeoples(int_id,string_name)
{
this.name=_name;
this.id=_id;
}
publicPeoples(string_name)
{
this.name=_name;
}
publicPeoples()
{
}
[TableColumn("PeopleID",true),PrimaryKey(AutoGenerated=true)]
publicintPeopleID
{
get{returnthis.id;}
set{this.id=value;}
}
[TableColumn("PeopleName",true)]
publicstringPeopleName
{
get{returnthis.name;}
set{this.name=value;}
}
}
}