就是用值对象来操作数据库的增删改查
每个值对象需要一个XML文件
连接数据库有个主XML文件
-------------------------------------------------------------------------------------------------
如下是主XML文件 用来指定数据库 和数据库的类型以及版本的
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
<!-- an ISessionFactory instance -->
<session-factory>
<!-- properties -->
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=KING;initial catalog=Student;Integrated Security=SSPI</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="use_outer_join">true</property>
<!-- mapping files -->
<mapping resource="HibDataGrid.Class.hbm.xml" assembly="HibDataGrid"/>
<mapping resource="HibDataGrid.Subject.hbm.xml" assembly="HibDataGrid"/>
</session-factory>
</hibernate-configuration>
-----------------------------------------------------------------------------------------------------
如下是值对象的XML文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="HibDataGrid">
<class name="Class,HibDataGrid" table="Class">
<id name="ClassId" column="ClassId" type="int" unsaved-value="0">
<generator class="native"/>
</id>
<property name="ClassName" column="ClassName" type="String"></property>
<property name="EntranceDate" column="EntranceDate" type="DateTime"></property>
<property name="Remark" column="Remark" type="String"></property>
</class>
</hibernate-mapping>
--------------------------------------------------------------------------------------------------------------
如下是这个XML的值对象类
using System;
namespace HibDataGrid
{
/// <summary>
/// Item 的摘要说明。
/// </summary>
public class Class
{
int classId;
string className;
DateTime entranceDate;
string remark;
public Class()
{
}
public int ClassId
{
get{return classId;}
set{classId=value;}
}
public string ClassName
{
get{return className;}
set{className=value;}
}
public DateTime EntranceDate
{
get{return entranceDate;}
set{entranceDate=value;}
}
public string Remark
{
get{return remark;}
set{remark=value;}
}
}
}
--------------------------------------------------------------------------------------------------------------
如下是绘画工厂的类 (我也不明白什么意思)
using System;
using NHibernate.Cfg;
using NHibernate;
namespace HibDataGrid
{
/// <summary>
/// HibernateHelper 的摘要说明。
/// </summary>
public class HibernateHelper
{
private static ISessionFactory sessionFactory;
static HibernateHelper()
{
sessionFactory = new Configuration().Configure().BuildSessionFactory();
}
public static ISessionFactory GetSessionFactory()
{
return sessionFactory;
}
}
}
-------------------------------------------------------------------------------------------------------
如下是 用来保存一个值对象进数据库的Add方法 问题就是这里 会报错
using System;
using NHibernate;
using NHibernate.Type;
using System.Collections;
namespace HibDataGrid
{
/// <summary>
/// HibernateUtil 的摘要说明。
/// </summary>
public class HibernateUtil
{
public HibernateUtil()
{
}
public void AddObject(Object obj)
{
ISessionFactory sessionFactory=HibernateHelper.GetSessionFactory();
ISession session=sessionFactory.OpenSession();
ITransaction tx = session.BeginTransaction();
session.Save(obj);
//就是这里 未处理的“NHibernate.ADOException”类型的异常出现在nhibernate.dll 中。其他信息: could not insert: [HibDataGrid.Class]
tx.Commit();
session.Close();
}
--------------------------------------------------------------------------------------------
调用Add方法的事件
private void button4_Click(object sender, System.EventArgs e)
{
HibernateUtil util=new HibernateUtil();
Class cs=new Class();
cs.ClassId=int.Parse(textBox1.Text.Trim());
cs.ClassName=textBox2.Text.Trim();
cs.EntranceDate=dtp1.Value;
cs.Remark=textBox4.Text.Trim();
util.AddObject(cs);
}