惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
博客园_首页
IT之家
IT之家
Engineering at Meta
Engineering at Meta
量子位
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LangChain Blog
爱范儿
爱范儿
WordPress大学
WordPress大学
F
Full Disclosure
T
Tailwind CSS Blog
GbyAI
GbyAI
Recorded Future
Recorded Future
美团技术团队
S
SegmentFault 最新的问题
A
About on SuperTechFans
小众软件
小众软件
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
Apple Machine Learning Research
Apple Machine Learning Research
D
DataBreaches.Net
J
Java Code Geeks
The Cloudflare Blog
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
D
Docker
Vercel News
Vercel News
H
Help Net Security
博客园 - 叶小钗
B
Blog
阮一峰的网络日志
阮一峰的网络日志
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
The Register - Security
The Register - Security
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
Jina AI
Jina AI

博客园 - pysharp

openstack horizon api step by step understanding horizon's template path what can PsTools\psexec do! - pysharp What's New in Python3.0 - pysharp [转]利用adsutil.vbs脚本创建自定义web站点 我的2008. sql基础篇,不断更新中...... TypeConvert Demo javascript 调试器v1.0.0.0 Dundas Chart Demo For New User - pysharp c#面向对象中的继承初步认识 .net 集合类初步认识 c# 目录操作类 c# 文件操作类 - pysharp - 博客园 关于urlrewrite的小DEMO - pysharp - 博客园 c# 动态编译方法 c# 读取Excel到datable asp.net 下载和在线预览Excel的方法 排序算法 c#实现
简单使用nHibernate,新手练习用。
pysharp · 2008-01-08 · via 博客园 - pysharp

最近公司有个项目要用的到nhibernate,因此用了一上午时间熟悉这个东东,对于我这个程序新手来说,第一次接触ORMapping,在熟悉过程中难免会犯一些错误。特记录再次,本文不涉及底层内容,一个仅用了一天的人来谈底层,会被人笑话的,呵呵。

这是在网上看的别人得一些文章的小总结了。

创建测试数据表。
CREATE TABLE users (
  LogonID nvarchar(20) NOT NULL default '0',
  Name nvarchar(40) default NULL,
  Password nvarchar(20) default NULL,
  EmailAddress nvarchar(40) default NULL,
  LastLogon datetime default NULL
)
go

创建一个web项目,然后添加一个类库项目YangTest.Entity。

映射数据表。我用的是MyGeneration的自动生成工具,具体下载google一下吧。

再此要强调一点,网上很多文章在讲到手动创建试题类的时候,都没有给属性加Virtual关键字,这时必须的,小错误容易被人忽视啊。

public class user
    {
        private string id;
        public virtual string LogOnID
        {
            get { return id; }
            set { id = value; }
        }

        private string _name;
        public virtual string name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _Password;
        public virtual string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        private string _Emailadress;
        public virtual string Emailadress
        {
            get { return _Emailadress; }
            set { _Emailadress = value; }
        }

        private DateTime? _Lastlogon;
        public virtual DateTime? Lastlogon
        {
            get { return _Lastlogon; }
            set { _Lastlogon = value; }
        }

映射文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="YangTest.Entity.user,YangTest.Entity" table="users" lazy="true">

    <id name="LogOnID" column="LogOnID" type="String">
      <generator class="assigned"/>
    </id>
    <property column="name" type="String" name="name" length="40" />
    <property column="Password" type="String" name="Password" length="20" />
    <property column="Emailadress" type="String" name="Emailadress" length="40" />
    <property column="Lastlogon" type="DateTime" name="Lastlogon" />
  </class>
</hibernate-mapping>

这样就完成了前期的准备工作。

现在我们才真正开始体验Nhibernate的便捷之处。

编辑我们的web项目的web.config文件,添加nhibernate配置

<configSections>
    <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System,
     Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </configSections>
 
    <nhibernate>
      <add
        key="hibernate.connection.provider"
        value="NHibernate.Connection.DriverConnectionProvider"
  />
      <add
        key="hibernate.connection.driver_class"
        value="NHibernate.Driver.SqlClientDriver"
  />
      <add
        key="hibernate.connection.connection_string"
        value="Server=192.168.0.106;database=YangTest;uid=sa;pwd=xinwei"
  />
      <add
        key="hibernate.connection.isolation"
        value="ReadCommitted"
  />
      <add
        key="hibernate.dialect"
        value="NHibernate.Dialect.MsSql2000Dialect"
  />
      <add key="show_sql" value="true"/>           //这里是看的别人的一个帖子,可以监视sql语句

    </nhibernate>

我是直接写在web.config里头的,没有单独建一个config。

这样双击aspx页面的button处理一下事件,体验一下nhibernate如何工作的。

using NHibernate;
using NHibernate.Cfg;      //记得添加引用啊
using YangTest.Entity;     

protected void btnAdd_Click(object sender, EventArgs e)
        {            
            cfg = new NHibernate.Cfg.Configuration();
            cfg.AddAssembly("YangTest.Entity");
            sessions = cfg.BuildSessionFactory();
            session = sessions.OpenSession();
            YangTest.Entity.user user = new user();
            user.LogOnID = "1";
            user.name = "Jack";
            user.Password = "654321";
            user.Emailadress = "jack@163.com";
            user.Lastlogon = DateTime.Now;
            session.Save(user);
            session.Flush();
            session.Close();
        }
好了,数据添加进去了,省去了自己写SQL语句的麻烦,这样就可以把精力集中起来处理业务流程的问题了,又提高生产率了,嘿嘿。

好了,就这么多了,我是新手,只知道这么多了,以后熟悉了之后可能会写深入一点。