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

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - surprise

首次使用Ajax 大家看一下这是.net的漏洞还是程序有问题,我一时也没搞清,只知道这问题挺怪的 - surprise - 博客园 一道Sql语句问题? 运用C#处理lob数据类型 (Oracle) 介绍一种Web上打印技术 Sql Server 中image类型迁移到Oracle 中Blob类型出现图片显示不出来,why????请博客们帮忙 在C#中存储Blob类型的数据, windows 2000下最多可以承受有多大容量外存???? 如何使用一个不错的图表组件WebChart(免费) 太爽了博客园有论坛了? 谁能帮我 WebChart组件的使用??? 能不能在博客园内建立一块开源区呀??? 如何在类库项目中添加配置文件?????? 中文版的hibernate pdf教程下载 介绍Nhibernate网站 俺现在碰到一些问题请大家帮忙??? 俺现在有个程序需要UTC转换为当地时间,哪位大虾会呀??? 我想问一下NHibernate的问题??? 10.1我发现一个非常好的架构NHibernate,我想和大家一起研究
用Nhibernate怎么实现数据的添加、删除、修改简单程序
surprise · 2004-10-13 · via 博客园 - surprise

Nhibernate怎么实现数据的添加、删除、修改简单程序

一、创建数据库

数据库名:Nhibernate
u
se NHibernate
go
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,
  PRIMARY KEY  (LogonID)
)
go

数据表:users

二、总体介绍

项目名:WebNhibernate

界面:WebForm.aspx

具体表现文件:WebForm.aspx.cs

实体类文件:EntityClass.cs

映射文件:Userhbm.xml

配置文件:Web.config

三、创建Web界面

类型

对象名

Text属性值

Label

Label1

ID:

Label

Label2

姓名:

Label

Label3

密码:

Label

Label4

Email

Label

Labmessage

TextBox

TxtId

TextBox

TxtName

TextBox

TxtPassword

TextBox

TxtEmail

Button

ButSave

添加

Button

ButDel

删除

Button

ButUpdata

修改

四、创建映射文件(xml文件)和实体类

实体类

using System;

namespace WebNhibernate

{

     public class EntityClass

     {

          private string id;

          private string userName;

          private string password;

          private string emailAddress;

         public EntityClass()

         {}

         public string Id

         {

              get { return id; }

              set { id = value; }

         }

         public string UserName

         {

              get { return userName; }

              set { userName = value; }

         }

         public string Password

         {

              get { return password; }

              set { password = value; }

         }

         public string EmailAddress

         {

              get { return emailAddress; }

              set { emailAddress = value; }

         }

     }

}

映射文件:

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

     <class name="WebNhibernate.EntityClass, WebNhibernate" table="users">

         <id name="Id" column="LogonId" type="String" length="20">

              <generator class="assigned" />

         </id>

          <property name="UserName" column= "Name" type="string" length="40"/>

          <property name="Password" type="string" length="20"/>

          <property name="EmailAddress" type="String" length="40"/>

     </class>

</hibernate-mapping>

注意点:

1<class name="WebNhibernate.EntityClass, WebNhibernate" table="users">

       WebNhibernate.EntityClass代表:实体类名

    WebNhibernate代表:该项目的装配集名称

      Users代表:数据表名

2.当属性列表<property name=”” column=””/>中既有name和column说明实体层的属性与数据表的字段名不同名

3.指定一个id, 在数据表中就是主键, 这个非常重要,nhibernate就是通过id来判断对象的唯一性的.

五、在配置文件中添加配置内容

1.首先在配置文件的<configuration>代码下面添加如下代码

       <configSections>

             <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />

     </configSections> 

     这一段代码是必须要的

2.在配置文件的</system.web>代码下面添加如下代码

<nhibernate>

         <!连接数据提供者 -->

         <add

              key="hibernate.connection.provider"         

              value="NHibernate.Connection.DriverConnectionProvider"

         />

         <!连接数据方言最常用的是MsSql2000Dialect -->

         <add

              key="hibernate.dialect"                     

              value="NHibernate.Dialect.MsSql2000Dialect"

         />

         <!连接数据驱动类-->

         <add

              key="hibernate.connection.driver_class"         

              value="NHibernate.Driver.SqlClientDriver"

         />

         <!连接数据库-->

         <add

              key="hibernate.connection.connection_string"

              value="Server=yanfa1;initial catalog=nhibernate;User id=sa;password=8626798;"

         />

</nhibernate>

六、实现代码

首先在文件头添加代码
using NHibernate;
using NHibernate.Cfg;
1.添加数据:

双击“添加“按钮

        private void ButSave_Click(object sender, System.EventArgs e)

         {

              mCfg=new Configuration();//创建配置类

              mCfg.AddXmlFile (System.Web.HttpContext.Current.Server.MapPath("Userhbm.xml"));//指明映射文件Userhbm.xml

              EntityClass vEntity=new EntityClass();

              vEntity.Id=TxtId.Text;

              vEntity.UserName=TxtName.Text;

              vEntity.Password=TxtPassword.Text;

              vEntity.EmailAddress=TxtEmail.Text;

              ISession vSession= mCfg.BuildSessionFactory().OpenSession();//创建会话工厂, 一般来说应该使用一个单例对象来封装会话工厂.

              ITransaction vTransaction = vSession.BeginTransaction();//创建事物处理

              try

              {

                   vSession.Save(vEntity);//向数据库添加数据

                   vTransaction.Commit();

                   Labmessage.Text="OK";

              }

              catch(Exception ex)

              {

                   vTransaction.Rollback();

                   Labmessage.Text="Error"+ex.ToString();

              }

              finally

              {

                   vSession.Close();

              }

         }

2.删除数据:

双击“删除“按钮
        private void ButDel_Click(object sender, System.EventArgs e)

         {

              mCfg=new Configuration();

              mCfg.AddXmlFile (System.Web.HttpContext.Current.Server.MapPath("Userhbm.xml"));

              ISession vSession= mCfg.BuildSessionFactory().OpenSession();

              ITransaction vTransaction = vSession.BeginTransaction();

              try

              {

                   EntityClass vEntity=(EntityClass) vSession.Load(typeof(EntityClass),TxtId.Text);//查找数据表中所要记录

                   vSession.Delete(vEntity);//向数据库删除数据

                   vTransaction.Commit();

                   Labmessage.Text="OK";

              }

              catch(Exception ex)

              {

                   vTransaction.Rollback();

                   Labmessage.Text="Error";

              }

              finally

              {

                   vSession.Close();

              }

         }

3.修改代码:

双击“修改“按钮
        private void ButUpdata_Click(object sender, System.EventArgs e)

         {

              mCfg=new Configuration();

              mCfg.AddXmlFile (System.Web.HttpContext.Current.Server.MapPath("Userhbm.xml"));

              ISession vSession= mCfg.BuildSessionFactory().OpenSession();

              ITransaction vTransaction = vSession.BeginTransaction();

              try

              {

                   EntityClass vEntity=(EntityClass) vSession.Load(typeof(EntityClass),TxtId.Text);

                   vEntity.UserName=TxtName.Text;

                   vEntity.Password=TxtPassword.Text;

                   vEntity.EmailAddress=TxtEmail.Text;

                   vSession.Update(vEntity); //向数据库修改数据

                   vTransaction.Commit();

                   Labmessage.Text="OK";

              }

              catch(Exception ex)

              {

                   vTransaction.Rollback();

                   Labmessage.Text="Error";

              }

              finally

              {

                   vSession.Close();

              }

         }

因本人也是刚接触Nhibernate不久,还有好多技术难点不怎么明白,还需多加努力,愿与大家一起探讨。