用Nhibernate怎么實(shí)現(xiàn)數(shù)據(jù)的添加、刪除、修改簡單程序
2024-07-21 02:23:32
供稿:網(wǎng)友
一、創(chuàng)建數(shù)據(jù)庫
數(shù)據(jù)庫名:nhibernate
use 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
數(shù)據(jù)表:users
二、總體介紹
項(xiàng)目名:webnhibernate
界面:webform.aspx
具體表現(xiàn)文件:webform.aspx.cs
實(shí)體類文件:entityclass.cs
映射文件:userhbm.xml
配置文件:web.config
三、創(chuàng)建web界面
類型
對(duì)象名
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
修改
四、創(chuàng)建映射文件(xml文件)和實(shí)體類
實(shí)體類
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>
注意點(diǎn):
1.<class name="webnhibernate.entityclass, webnhibernate" table="users">
webnhibernate.entityclass代表:實(shí)體類名
webnhibernate代表:該項(xiàng)目的裝配集名稱
users代表:數(shù)據(jù)表名
2.當(dāng)屬性列表<property name=”” column=””/>中既有name和column說明實(shí)體層的屬性與數(shù)據(jù)表的字段名不同名
3.指定一個(gè)id, 在數(shù)據(jù)表中就是主鍵, 這個(gè)非常重要,nhibernate就是通過id來判斷對(duì)象的唯一性的.
五、在配置文件中添加配置內(nèi)容
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>
<!—連接數(shù)據(jù)提供者 -->
<add
key="hibernate.connection.provider"
value="nhibernate.connection.driverconnectionprovider"
/>
<!—連接數(shù)據(jù)方言最常用的是mssql2000dialect -->
<add
key="hibernate.dialect"
value="nhibernate.dialect.mssql2000dialect"
/>
<!—連接數(shù)據(jù)驅(qū)動(dòng)類-->
<add
key="hibernate.connection.driver_class"
value="nhibernate.driver.sqlclientdriver"
/>
<!—連接數(shù)據(jù)庫-->
<add
key="hibernate.connection.connection_string"
value="server=yanfa1;initial catalog=nhibernate;user id=sa;password=8626798;"
/>
</nhibernate>
六、實(shí)現(xiàn)代碼
首先在文件頭添加代碼
using nhibernate;
using nhibernate.cfg;
1.添加數(shù)據(jù):
雙擊“添加“按鈕
private void butsave_click(object sender, system.eventargs e)
{
mcfg=new configuration();//創(chuàng)建配置類
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();//創(chuàng)建會(huì)話工廠, 一般來說應(yīng)該使用一個(gè)單例對(duì)象來封裝會(huì)話工廠.
itransaction vtransaction = vsession.begintransaction();//創(chuàng)建事物處理
try
{
vsession.save(ventity);//向數(shù)據(jù)庫添加數(shù)據(jù)
vtransaction.commit();
labmessage.text="ok";
}
catch(exception ex)
{
vtransaction.rollback();
labmessage.text="error"+ex.tostring();
}
finally
{
vsession.close();
}
}
2.刪除數(shù)據(jù):
雙擊“刪除“按鈕
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);//查找數(shù)據(jù)表中所要記錄
vsession.delete(ventity);//向數(shù)據(jù)庫刪除數(shù)據(jù)
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); //向數(shù)據(jù)庫修改數(shù)據(jù)
vtransaction.commit();
labmessage.text="ok";
}
catch(exception ex)
{
vtransaction.rollback();
labmessage.text="error";
}
finally
{
vsession.close();
}
}
因本人也是剛接觸nhibernate不久,還有好多技術(shù)難點(diǎn)不怎么明白,還需多加努力,愿與大家一起探討。