單表使用
建立一下表
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();
}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
