数据库使用Access,使用Log4net纪录日志。
1、首先是web.config的配置,将NHibernate注册进系统
<?xml version="1.0"?> <configuration> ?<configSections> ??<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/> ?</configSections> ?<appSettings> ??<add key="NhibernateConfigFile" value="~/hibernate.cfg.xml"/> ?</appSettings> ?<connectionStrings/> ?<system.web> ??<compilation debug="true"> ???<assemblies> ????<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> ????<add assembly="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> ???</assemblies> ??</compilation> ??<authentication mode="Windows"/> ?</system.web> </configuration>
2、然后是Nhibernate的具体配置,这里使用Access数据库:hibernate.cfg.xml
Nhibernate支持N种数据库,可以自由选择需要的模块,在这里主要设置数据连接信息、使用的数据操作组件、以及纪录SQl语句、使用命名空间等信息。
<?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration? xmlns="urn:nhibernate-configuration-2.2" > ?<session-factory name="VeryCodes.NHibernate.Access"> ??<!-- properties --> ??<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> ??<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> ??<property name="connection.driver_class">NHibernate.JetDriver.JetDriver, NHibernate.JetDriver</property> ??<property name="dialect">NHibernate.JetDriver.JetDialect, NHibernate.JetDriver</property> ??<property name="connection.connection_string">Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\wwwroot\verycodes\NhibernateTest\App_Data\db1.mdb</property> ??<property name="show_sql">true</property> ??<property name="use_outer_join">true</property> ??<property name="use_proxy_validator">false</property> ??<mapping assembly="VeryCodes.NHibernateData"/> ?</session-factory> </hibernate-configuration>
3、log4net的配置
<?xml version="1.0" encoding="utf-8" ?> <log4net> ?<appender name="rollingFile" type="log4net.Appender.RollingFileAppender"> ??<param name="File" value="log.txt" /> ??<param name="AppendToFile" value="false" /> ??<param name="RollingStyle" value="Date" /> ??<param name="DatePattern" value="yyyy.MM.dd" /> ??<param name="StaticLogFileName" value="true" /> ??<layout type="log4net.Layout.PatternLayout"> ???<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" /> ??</layout> ?</appender> ?<root> ??<!--如果只需要看看Sql设置INFO就够了,如果你要调试可以设置为DEBUG或ALL--> ??<priority value="INFO" /> ??<appender-ref ref="rollingFile" /> ?</root> </log4net>
4、看一下Access表结构
表名:Cat
字段:CatID、Name、Sex、Weight、CatClassID
方便起见,全部设为文本格式
5、接着是Cat.cs实体类
using System; using System.Collections.Generic; using System.Text; namespace VeryCodes.NhibernateData { ??? public class Cat ??? { ??????? private string catID; ??????? private string name; ??????? private string sex; ??????? private string weight; ??????? public Cat() ??????? { ??????? } ??????? public string CatID ??????? { ??????????? get { return catID; } ??????????? set { catID = value; } ??????? } ??????? public string Name ??????? { ??????????? get { return name; } ??????????? set { name = value; } ??????? } ??????? public string Sex ??????? { ??????????? get { return sex; } ??????????? set { sex = value; } ??????? } ??????? public string Weight ??????? { ??????????? get { return weight; } ??????????? set { weight = value; } ??????? } ??? } }
6、还需要创建一个映射文件,将实体对象与数据库表对应
这个文件要设置为:嵌入的资源
其中id节点指定了主键,并设置主键生成方式uuid.hex
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> ?<class name="VeryCodes.NhibernateData.Cat,VeryCodes.NhibernateData" table="Cat" lazy="false"> ??<id name="CatID"> ???<column name="CatID" sql-type="char(32)" not-null="true"/> ???<generator /> ??</id> ??<property name="Name" column="Name" type="string" length="16" not-null="true" /> ??<property name="Sex" column="Sex" type="string" length="4" /> ??<property name="Weight" column="Weight" type="string" length="4" /> ?</class> </hibernate-mapping>
7、添加数据
?//创建一个实体对象 ??????? Cat princess = new Cat(); ??????? //设置对象的属性 ??????? princess.Name = CatName.Text; ??????? princess.Sex = CatSex.Text; ??????? princess.Weight = CatWeight.Text; ??????? //获取一个Session ??????? session = NHibernateHelper.GetCurrentSession(); ??????? //开启事务 ??????? ITransaction transaction = session.BeginTransaction(); ??????? try ??????? { ??????????? //保存数据 ??????????? session.Save(princess); ??????????? //提交事务 ??????????? transaction.Commit(); ??????? } ??????? catch (Exception ex) ??????? { ??????????? //回滚 ??????????? transaction.Rollback(); ??????????? throw ex; ??????? } ??????? finally ??????? { ??????????? //关闭Session ??????????? NHibernateHelper.CloseSession(); ??????? }
8、修改数据
? //创建一个实体对象 ??????? Cat princess = new Cat(); ??????? //设置对象的属性 ??????? princess.CatID = CatId.Value; ??????? princess.Name = CatName.Text; ??????? princess.Sex = CatSex.Text; ??????? princess.Weight = CatWeight.Text; ??????? //获取一个Session ??????? session = NHibernateHelper.GetCurrentSession(); ??????? //开启事务 ??????? ITransaction transaction = session.BeginTransaction(); ??????? try ??????? { ??????????? //保存数据 ??????????? session.Update(princess); ??????????? //提交事务 ??????????? transaction.Commit(); ??????? } ??????? catch (Exception ex) ??????? { ??????????? //回滚 ??????????? transaction.Rollback(); ??????????? throw ex; ??????? } ??????? finally ??????? { ??????????? //关闭Session ??????????? NHibernateHelper.CloseSession(); ??????? }
9、删除数据
string strCatId = Request.QueryString["id"].ToString(); Cat getCat = (Cat)session.Load(typeof(Cat), strCatId); session.Delete(getCat);
10、查询数据
//条件查询 ??????????? //IQuery query = session.CreateQuery("select c from Cat as c where c.Sex = :sex"); ??????????? //query.SetString("sex", "F"); ??????????? IQuery query = session.CreateQuery("select c from Cat as c"); ??????????? sb.Append("<table>"); ??????????? //循环输出 ??????????? foreach (Cat cat in query.Enumerable()) ??????????? { ??????????????? sb.Append("<tr><td>" + cat.Name + "</td><td>" + cat.Sex + "</td><td>" + cat.Weight + "</td><td><a href='default.aspx?action=update&id=" + cat.CatID + "'>修改</a> <a href='default.aspx?action=delete&id=" + cat.CatID + "'>删除</a></td></tr>"); ??????????? } ??????????? sb.Append("</table>");
OK,实现了单表增、删、改、查的操作。
关键字: log4net Nhibernate
发表评论
相关文章
国内AI资源汇总,AI聊天、AI绘画、AI写作、AI视频、AI设计、AI编程、AI音乐等,国内顺畅访问,无需科学上网。
扫码或点击进入:萤火AI大全
文章分类
最新评论