开发平台:sql2005 .net2005 NHibernate-2.1.2.GA-bin OrmCodeGenerator.exe
数据库代码 建立Demo库然后建立表 create table demotable(id int primary key not null identity(1,1),upsize_ts timestamp null)
用OrmCodeGenerator.exe生成Nhibernate代码
生成后的文件分别为 hibernate.cfg.xml IDAL/Idemotable.cs HBMXML/demotable.hbm.xml demotable.cs
demotable.cs 文件内容
using System;
namespace Model
{
public class demotable
{
private int M_id;
private byte[] M_upsize_ts;
public virtual int id
{
get { return M_id; }
set { M_id = value; }
}
public virtual byte[] upsize_ts
{
get { return M_upsize_ts; }
set { M_upsize_ts = value; }
}
}
}
HBMXML/demotable.hbm.xml文件内容
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true">
<class name="Model.demotable, AppModel" table="demotable" dynamic-update="true">
<id name="id" column="id" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>
<property name="upsize_ts" type="Byte[]" generated="always">
<column name="upsize_ts" not-null="false" sql-type="timestamp"/>
</property>
</class>
</hibernate-mapping>
注:这里的upsize_ts经过修改了!这样写才可以保证对sql2005中的timestamp类型的映射。
IDAL/Idemotable.cs文件内容
using System;
using System.Collections.Generic;
using Model;
namespace IDAL
{
public interface Idemotable
{
IList<demotable> getAlldemotable();
demotable getOnedemotable(string i_demotablekey);
void insertdemotable(demotable i_objdemotable);
void updatedemotable(demotable i_objdemotable);
void deletedemotable(demotable i_objdemotable);
}
}
hibernate.cfg.xml 文件内容
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=127.0.0.1;initial catalog=Demo;User Id=sa;Password=666666;</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="use_outer_join">true</property>
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="AppModel" />
</session-factory>
</hibernate-configuration>
注:这里数据库连接字符串设置成你的数据库字符串。
准备工作作好了。
打开.net2005新建解决方案,分别添加项目Model,DAL,别外在新建一个网站website1。
将 HBMXML/demotable.hbm.xml demotable.cs 添加到Model项目中并设置demotable.hbm.xml 文件为“嵌入的资源”,修改项目程序集名称为AppModel,生成程序集。
将 IDAL/Idemotable.cs 添加到 DAL项目中,程序集名称为DAL,新建一个类DemoPojo 。添加NHibernate程序集。本项目用的是Hibernate.ByteCode.Castle.ProxyFactoryFactory工厂类,将其相关的程序集全部添加进项目。
DemoPojo .cs内容为
using System;
using Model;
using NHibernate;
using NHibernate.Cfg;
namespace DAL
{
public class DemoPojo:IDAL.Idemotable
{
private static readonly Configuration cfg = new Configuration();
private static readonly ISessionFactory sessionFactory = cfg.Configure().BuildSessionFactory();
ISession sess;
public DemoPojo() {
sess = sessionFactory.OpenSession();
}
#region Idemotable 成员
public System.Collections.Generic.IList<demotable> getAlldemotable()
{
return sess.CreateQuery("from demotable").List<Model.demotable>();
}
public demotable getOnedemotable(string i_demotablekey)
{
throw new Exception("The method or operation is not implemented.");
}
public void insertdemotable(demotable i_objdemotable)
{
sess.Save(i_objdemotable);
}
public void updatedemotable(demotable i_objdemotable)
{
throw new Exception("The method or operation is not implemented.");
}
public void deletedemotable(demotable i_objdemotable)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
}
代码仅实现了getAlldemotable()方法和insertdemotable(demotable i_objdemotable)方法
生成程序集DAL。
将DAL项目里bin文件夹下的所有dll添加到网站website1项目,设置网站为启动项目。
Default.aspx文件内容:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
Default.aspx.cs文件内容:
using System;
using System.Data;
using System.Configuration;
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 System.Text;
using Model;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Model.demotable demo = new Model.demotable();
demo.upsize_ts = (new UnicodeEncoding()).GetBytes(DateTime.Now.ToString());
IDAL.Idemotable idemo = new DAL.DemoPojo();
idemo.insertdemotable(demo);
IList<Model.demotable> array = (new DAL.DemoPojo()).getAlldemotable();
GridView1.DataSource = array;
GridView1.DataBind();
for (int i = 1; i < array.Count; i++)
{
if (array[0].upsize_ts == array[i].upsize_ts)
Response.Write("重复" + i+"<br>");
else
Response.Write("未重复"+i+"<br>");
}
}
}
所有项目建立完毕,按Ctrl+F5运行,
default.aspx页面显示内容为
未重复1
未重复2
未重复3
未重复4
id |
---|
1 |
2 |
3 |
4 |
5 |
-------------------------------------------------------------------------------------
到此成功完成!!!!
小弟技术有限,如有错误请各位大虾多多指教。