NHibernate插入数据的列子
2012-05-18 17:39 zhishuru 阅读(348) 评论(0) 编辑 收藏 举报话不多说,直接贴源码:1.Model:public virtual int LogonID { get; set; }
public virtual string Name1 { get; set; }
public virtual string Password1 { get; set; }
public virtual string EmailAddress { get; set; }
public virtual DateTime LastLogon { get; set; }
2.User.hbm.xml:<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<!--1.2版的NHibernate为2.2-->
<class name="Model.User,Model" table="Users2">
<!--name的前面是命名空间+类命,后面是类库名称-->
<id name="LogonID" column="LogonID" unsaved-value="0">
<generator class="assigned"/>
</id>
<property name="Name1" column="Name1" ></property>
<property name="Password1" column="Password1" ></property>
<property name="EmailAddress" column="EmailAddress" ></property>
<property name="LastLogon" column="LastLogon"></property>
</class>
</hibernate-mapping>
3.web.config配置下的cs后台代码:NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.AddAssembly("Model");
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
User newUser = new User();
newUser.LogonID =35;
newUser.Name1 = "李四";
newUser.Password1 = "1234";
newUser.EmailAddress = "131552";
newUser.LastLogon =DateTime.Now;
session.Save(newUser);
session.Flush();
4.oracle10g的web.config配置文件:<configSections>
<section
name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
/>
</configSections>
<!-- Add this element -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.connection_string">
User ID=CSU;Password=123456;Data Source=CSUGRA
</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="hbm2ddl.auto">update</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<mapping assembly="Model"/>
</session-factory>
</hibernate-configuration>
5.最后来张贴图: