当前位置:编程学习 > asp >>

NHibernate

create table t_user(id int primary key auto_increment, name char(20), pwd char(20));

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <!-- ISessionFactory实例 -->
  <session-factory>
    <property name="hbm2ddl.keywords">none</property>
    <!-- 属性 -->
    <!-- 设置connection provider,NHibernate将用之连接数据库 -->
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <!-- 设置所用driver,在此,我们为MySQL数据库选择MySqlDataDriver -->
    <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
    <!-- 设置连接到数据库的connection string -->
    <property name="connection.connection_string">Server=127.0.0.1;Database=test;Uid=root;Pwd=root;</property>


    <!-- 为特定数据库选择Dialect,这里选择使用MySQL5Dialect -->
    <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
    <!-- 映射文件 -->
    <mapping assembly="Test"></mapping>
  </session-factory>


</hibernate-configuration>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="MyNamespace.Data.Tuser, Test" table="t_user" lazy="true">
    <id name="Id"  type="int" >
      <column name="id"></column>
      <generator class="native" />
    </id>
    <property name="Name" type="String">
      <column name="name" length="20" sql-type="char" not-null="false"/>
    </property>
    <property name="Pwd" type="String">
      <column name="pwd" length="20" sql-type="char" not-null="false"/>
    </property>
  </class>
</hibernate-mapping>

 

using System;
using System.Collections;


namespace MyNamespace.Data
{
    #region Tuser


    /// <summary>
    /// Tuser object for NHibernate mapped table 't_user'.
    /// </summary>
    public class Tuser
    {
        #region Member Variables


        protected int _id;
        protected string _name;
        protected string _pwd;


        public Tuser() { }


        public virtual int Id
        {
            get { return _id; }
            set { _id = value; }
        }


        public virtual string Name
        {
            get { return _name; }
            set
            {
                if (value != null && value.Length > 10)
                    throw new ArgumentOutOfRangeException("Invalid value for Name", value, value.ToString());
                _name = value;
            }
        }


        public virtual string Pwd
        {
            get { return _pwd; }
            set
            {
                if (value != null && value.Length > 10)
                    throw new ArgumentOutOfRangeException("Invalid value for Pwd", value, value.ToString());
                _pwd = value;
            }
        }


        #endregion


    }


    #endregion
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using MyNamespace.Data;

 


namespace NHibernate
{
    class Program
    {
        static void Main(string[] args)
        {
            Tuser user = new Tuser();
            user.Name = "111";www.zzzyk.com
            user.Pwd = "222";
            Configuration cfg = new Configuration();
            ISessionFactory factory = cfg.Configure().BuildSessionFactory();
            ISession session = factory.OpenSession();
           Console.WriteLine("success" + session.GetType());
           session.SaveOrUpdate(user);
           IQuery q = session.CreateQuery("from Tuser");
           Console.WriteLine(q.List().Count);
           Console.ReadKey();
        }
    }
}
作者:GJL5787

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,