Castle ActiveRecord學習實踐(1):快速入門指南

單表使用html

創建一下表
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]
GOmysql

創建一個實體類,代碼以下
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;web

/// <summary>
/// Employees 的摘要說明
/// </summary>sql

[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
}app


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 錯誤頁
            以代替錯誤堆棧跟蹤。ide

        <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();}

相關文章
相關標籤/搜索