|
2#
楼主 |
发表于 2006-4-7 09:40:39
|
只看该作者
using System;
namespace DB
{
public class users
{
public users()
{
}
private System.String _Password;
public System.String Password
{
get { return _Password; }
set { _Password = value; }
}
private System.DateTime _LastLogon;
public System.DateTime LastLogon
{
get { return _LastLogon; }
set { _LastLogon = value; }
}
private System.String _Name;
public System.String Name
{
get { return _Name; }
set { _Name = value; }
}
private System.String _LogonID;
public System.String LogonID
{
get { return _LogonID; }
set { _LogonID = value; }
}
private System.String _EmailAddress;
public System.String EmailAddress
{
get { return _EmailAddress; }
set { _EmailAddress = value; }
}
}
}
我们使用另外一个类EntityControl来通过ORM的方法把这个类中的数据通过增删改的方法与数据库中的数据进行同步,这里我们只需要知道它有这个功能就足够。
using System;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;
using NHibernate;
using NHibernate.Type;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Tool.hbm2ddl;
using System.Collections;
namespace DB
{
///
/// Summary description for UsersControl.
///
public class EntityControl
{
private static EntityControl entity;
private static ISessionFactory sessions;
private static Configuration cfg;
private static Dialect dialect;
public static EntityControl CreateControl()
{
if (entity == null)
{
BuildSessionFactory();
if (entity == null)
entity = new EntityControl();
}
return entity;
}
private static void BuildSessionFactory()
{
ExportSchema( new string[] { "users.hbm.xml"
, "Department.hbm.xml"
, "Employee.hbm.xml"
} , false);
}
public void AddEntity(object entity)
{
ISession s = sessions.OpenSession();
ITransaction t = s.BeginTransaction();
try
{
s.Save(entity);
t.Commit();
}
catch(Exception e)
{
t.Rollback();
throw e;
}
finally
{
s.Close();
}
}
public void UpdateEntity(object entity,object key)
{
ISession s = sessions.OpenSession();
ITransaction t = s.BeginTransaction();
try
{
s.Update(entity,key);
t.Commit();
}
catch(Exception e)
{
t.Rollback();
throw e;
}
finally
{
s.Close();
}
}
public void DeleteEntity(object entity)
{
ISession s = sessions.OpenSession();
ITransaction t = s.BeginTransaction();
try
{
s.Delete(entity);
t.Commit();
}
catch(Exception e)
{
t.Rollback();
throw e;
}
finally
{
s.Close();
}
}
public object GetEntity(System.Type theType, object id)
{
object obj;
ISession s = sessions.OpenSession();
ITransaction t = s.BeginTransaction();
obj = s.Load( theType, id);
t.Commit();
s.Close();
return obj;
}
public IList GetEntities(string query)
{
IList lst;
ISession s = sessions.OpenSession();
ITransaction t = s.BeginTransaction();
lst = s.Find(query);
t.Commit();
s.Close();
return lst;
}
public IList GetEntities(string query, object value, IType type)
{
IList lst;
ISession s = sessions.OpenSession();
ITransaction t = s.BeginTransaction();
lst = s.Find(query,value,type);
t.Commit();
s.Close();
return lst;
}
#region "Schema deal"
private static void ExportSchema(string[] files)
{
ExportSchema(files, true);
}
private static void ExportSchema(string[] files, bool exportSchema)
{
cfg = new Configuration();
for (int i=0; i
{
cfg.AddResource("DB." + files, Assembly.Load("DB"));
}
if(exportSchema) new SchemaExport(cfg).Create(true, true);
sessions = cfg.BuildSessionFactory( );
dialect = NHibernate.Dialect.Dialect.GetDialect();
}
///
/// Drops the schema that was built with the TestCase's Configuration.
///
private static void DropSchema()
{
new SchemaExport(cfg).Drop(true, true);
}
private static void ExecuteStatement(string sql)
{
ExecuteStatement(sql, true);
}
private static void ExecuteStatement(string sql, bool error)
{
IDbConnection conn = null;
IDbTransaction tran = null;
try
{
if (cfg == null)
cfg = new Configuration();
NHibernate.Connection.IConnectionProvider prov = NHibernate.Connection.ConnectionProviderFactory.NewConnectionProvider(cfg.Properties);
conn = prov.GetConnection();
tran = conn.BeginTransaction();
IDbCommand comm = conn.CreateCommand();
comm.CommandText = sql;
comm.Transaction = tran;
comm.CommandType = CommandType.Text;
comm.ExecuteNonQuery();
tran.Commit();
}
catch(Exception exc)
{
if (tran != null)
tran.Rollback();
if (error)
throw exc;
}
finally
{
if (conn != null)
conn.Close();
}
}
#endregion
}
}
这两个类提供的方法,我们可以从下面的类图中看出来:
[ 本帖最后由 ecust 于 2006-4-7 09:42 编辑 ] |
|