1.
一个基本的示例
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> if exists(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>--> using System;
using Gentle.Framework;
namespace ConGentleStudy
{
/// <summary>
/// BaseExample的摘要说明。
/// </summary>
public class BaseExample
{
[STAThread]
static void Main( string []args)
{
Console.WriteLine( " 创建人实例。 " );
Peoplespeople = new Peoples( " xuanfeng " );
Console.WriteLine( " 创建人实例。 " );
Broker.Insert(people);
Console.WriteLine( " 创建人实例。 " );
people.PeopleName = " peizunyou " ;
Console.WriteLine( " 更新人的信息。 " );
Broker.Update(people);
Console.WriteLine( " 创建返回实例信息的Key! " );
Keykey = new Key( " Peoples " , typeof (Peoples), true , " PeopleName " , " peizunyou " );
Console.WriteLine(key.Count.ToString());
Console.WriteLine( " 创建人实例! " );
Peoplespeople1 = new Peoples(); // 必须先实例化申请到空间,下步才不会出错。
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 = new Key( typeof (Peoples), true , " PeopleID " ,people1.PeopleID);
Broker.Remove( typeof (Peoples),key_rem); // 主意,Key中提供的字段PeopleID是表中的主键,删除才有效!
Console.WriteLine( " 演示完成,按任何键退出! " );
Console.Read();
}
}
[TableName( " Peoples " )]
public class Peoples
{
string name;
int id;
public Peoples( int _id, string _name)
{
this .name = _name;
this .id = _id;
}
public Peoples( string _name)
{
this .name = _name;
}
public Peoples()
{
}
[TableColumn( " PeopleID " , true ),PrimaryKey(AutoGenerated = true )]
public int PeopleID
{
get { return this .id;}
set { this .id = value;}
}
[TableColumn( " PeopleName " , true )]
public string PeopleName
{
get { return this .name;}
set { this .name = value;}
}
}
}
通过该示例可以撑握以下内容:
1) Gentle.net 是怎么把对象(类)和关系数据库 (SQL) 关联起来的 .
2) 使用 Gentle.net 插入,更新,返回实例信息,和删除数据 .
本示例中以人为实例,包括人
ID
和姓名
.
附上建表脚本
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> if exists(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>--> using System;
using Gentle.Framework;
namespace ConGentleStudy
{
/// <summary>
/// BaseExample的摘要说明。
/// </summary>
public class BaseExample
{
[STAThread]
static void Main( string []args)
{
Console.WriteLine( " 创建人实例。 " );
Peoplespeople = new Peoples( " xuanfeng " );
Console.WriteLine( " 创建人实例。 " );
Broker.Insert(people);
Console.WriteLine( " 创建人实例。 " );
people.PeopleName = " peizunyou " ;
Console.WriteLine( " 更新人的信息。 " );
Broker.Update(people);
Console.WriteLine( " 创建返回实例信息的Key! " );
Keykey = new Key( " Peoples " , typeof (Peoples), true , " PeopleName " , " peizunyou " );
Console.WriteLine(key.Count.ToString());
Console.WriteLine( " 创建人实例! " );
Peoplespeople1 = new Peoples(); // 必须先实例化申请到空间,下步才不会出错。
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 = new Key( typeof (Peoples), true , " PeopleID " ,people1.PeopleID);
Broker.Remove( typeof (Peoples),key_rem); // 主意,Key中提供的字段PeopleID是表中的主键,删除才有效!
Console.WriteLine( " 演示完成,按任何键退出! " );
Console.Read();
}
}
[TableName( " Peoples " )]
public class Peoples
{
string name;
int id;
public Peoples( int _id, string _name)
{
this .name = _name;
this .id = _id;
}
public Peoples( string _name)
{
this .name = _name;
}
public Peoples()
{
}
[TableColumn( " PeopleID " , true ),PrimaryKey(AutoGenerated = true )]
public int PeopleID
{
get { return this .id;}
set { this .id = value;}
}
[TableColumn( " PeopleName " , true )]
public string PeopleName
{
get { return this .name;}
set { this .name = value;}
}
}
}