Castle Active Record for .NET2.0快速入门示例

单表使用

建立一下表
CREATE TABLE [dbo].[Employees] (
    [Employeesid] [int] IDENTITY (1, 1) NOT NULL ,
    [LogonName] [varchar] (40) COLLATE Chinese_PRC_CI_AS NULL ,
    [LastLogon] [datetime] NULL
) ON [PRIMARY]
GO

建立一个实体类,代码如下
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;
using Castle.ActiveRecord.Framework.Config;
using Castle.ActiveRecord.Framework.Internal;
using Castle.ActiveRecord.Framework.Scopes;
using Castle.ActiveRecord.Framework.Validators;
using Castle.ActiveRecord.Queries.Modifiers;
using Castle.ActiveRecord.Queries;

/// <summary>
/// Employees 的摘要说明
/// </summary>

[ActiveRecord("Employees")]
public class Employees : ActiveRecordBase
{
    private int _Employeesid;
    private string _LogonName;
    private DateTime _LastLogon;
    [PrimaryKey(PrimaryKeyType.Identity, "Employeesid")]
    public int Employeesid
    {
        get { return _Employeesid; }
        set { _Employeesid = value; }
    }
    [Property("LogonName")]
    public string LogonName
    {
        get { return _LogonName; }
        set { _LogonName = value; }
    }
    [Property("LastLogon")]
    public DateTime LastLogon
    {
        get { return _LastLogon; }
        set { _LastLogon = value; }
    }

    #region
    public static void DeleteAll()
    {
        DeleteAll(typeof(Employees));
    }
    public static IList FindAll()
    {
        return (IList)FindAll(typeof(Employees));
    }
    public static Employees Find(int Employeesid)
    {
        return (Employees)FindByPrimaryKey(typeof(Employees), Employeesid);
    }
    #endregion
}


web.config中加入以下
<?xml version="1.0"?>
<!--
    注意: 除了手动编辑此文件以外,您还可以使用
    Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
     “网站”->“Asp.Net 配置”选项。
    设置和注释的完整列表在
    machine.config.comments 中,该文件通常位于
    /Windows/Microsoft.Net/Framework/v2.x/Config 中
-->
<configuration>
 <configSections>
  <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord"/>
 </configSections>
 <appSettings/>
 <connectionStrings/>
 <system.web>
  <!--
            设置 compilation debug="true" 将调试符号插入
            已编译的页面中。但由于这会
            影响性能,因此只在开发过程中将此值
            设置为 true。
        -->
  <compilation debug="true">
   <assemblies>
    <add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
  <!--
            通过 <authentication> 节可以配置 ASP.NET 使用的
            安全身份验证模式,
            以标识传入的用户。
        -->
  <authentication mode="Windows"/>
  <!--
            如果在执行请求的过程中出现未处理的错误,
            则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
            开发人员通过该节可以配置
            要显示的 html 错误页
            以代替错误堆栈跟踪。

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
 </system.web>
 <activerecord>
  <config>
   <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
   <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
   <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
   <add key="hibernate.connection.connection_string" value="Data Source=192.168.108.123,3758;Initial Catalog=GameMiddleNew;Persist Security Info=True;User ID=testuser;pooling = true;Max Pool Size=50;Min Pool Size=3;Password=3Ger@jiubang;"/>
  </config>
 </activerecord>

<!--mysql如下配置-->

 <activerecord>
  <config>
   <add key="hibernate.connection.driver_class" value="NHibernate.Driver.MySqlDataDriver"/>
   <add key="hibernate.dialect" value="NHibernate.Dialect.MySQLDialect"/>
   <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
   <add key="hibernate.connection.connection_string" value="Database=mydb;Data Source=localhost;User Id=root;Password=sureme"/>
  </config>
 </activerecord>
</configuration>


在Global.asax的Application_Start添加初始化代码
    void Application_Start(object sender, EventArgs e)
    {
        // 在应用程序启动时运行的代码
        Castle.ActiveRecord.Framework.IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as Castle.ActiveRecord.Framework.IConfigurationSource;
        Castle.ActiveRecord.ActiveRecordStarter.Initialize(typeof(Employees).Assembly, source);
    }


使用
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<html>
<head id="Head1" runat="server">
    <title>Castle Active Record for 2.0快速入门示例</title>
</head>
<body>
    <form id="form1" runat="server">
     <h1>Castle Active Record for 2.0快速入门示例</h1>
        <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
            <Columns>
                <asp:BoundField HeaderText="Employee ID" DataField="Employeesid" />
                <asp:BoundField HeaderText="LogonName" DataField="LogonName" />
                <asp:BoundField HeaderText="LastLogon" DataField="LastLogon" />
            </Columns>
        </asp:GridView>
    </form>
</body>
</html>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource = Employees.FindAll();
        this.GridView1.DataBind();

        //增加
        Employees teste = new Employees();
        teste.LogonName = "test";
        teste.LastLogon = System.DateTime.Now;
        teste.Create();
        teste = (Employees)teste.SaveCopy();

        //修改
        teste.LogonName = "wo kao";
        teste.UpdateAndFlush();

        ////删除
        //Employees testf = new Employees();
        //testf.Employeesid = teste.Employeesid;
        //testf.Delete();
}

你可能感兴趣的:(Active)