public class UserAccess
{
//構造函數
public UserAccess()
{
cfg.AddAssembly("Entitys");
}
private NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
private ISession session = null; //會話工廠
private ITransaction tran = null; //事務處理
private string m_error = "";
/// <summary>
/// 獲取錯誤信息
/// </summary>
public string Error
{
get { return this.m_error; }
}
/// <summary>
/// 添加
/// </summary>
/// <returns></returns>
public bool InsertUser(login l)
{
try
{
session = cfg.BuildSessionFactory().OpenSession();
tran = session.BeginTransaction();
; session.Save(l);
tran.Commit();
}
catch (Exception ex)
{
tran.Rollback();
this.m_error = ex.Message;
return false;
}
finally
{
this.session.Close();
}
return true;
}
/// <summary>
/// 修改
/// </summary>
/// <returns></returns>
public bool UpdateUser(login l,int id)
{
try
{
session = cfg.BuildSessionFactory().OpenSession();
tran = session.BeginTransaction();
文章整理:學網 http://www.xue5.com (本站) [1] [2] [3] [4] [5] [6]
session.Update(l,id);
tran.Commit();
}
catch(Exception ex)
{
tran.Rollback();
this.m_error = ex.Message;
return false;
}
finally
{
&nbs; this.session.Close();
}
return true;
}
/// <summary>
/// 刪除
/// </summary>
/// <returns></returns>
public bool IDelUser(int ID)
{
try
{
session = cfg.BuildSessionFactory().OpenSession();
tran = session.BeginTransaction();
歡迎光臨學網,收藏本篇文章 [1] [2] [3] [4] [5] [6]
login l = (login)session.Load(typeof(login), ID);
session.Delete(l);
tran.Commit();
}
catch(Exception ex)
{
tran.Rollback();
this.m_error = ex.Message;
return false;
}
finally
{
this.session.Close();
}
return true;
}
/// <summary>
/// 查找一條數據
/// </summary>
/// <returns></returns>
public Entitys.login SelectUserByID(int ID)
{
try
{
session = cfg.BuildSessionFactory().OpenSession();
歡迎光臨學網,點擊這裡查看更多文章教程 [1] [2] [3] [4] [5] [6]
login l = (login)session.Load(typeof(login), ID);
return l;
}
catch (Exception ex)
{
this.m_error = ex.Message;
return null;
}
finally
{
this.session.Close();
}
}
///// <summary>
///// 查看
///// </summary>
///// <returns></returns>
//public int GetUsers()
//{
// try
// {
// }
// catch(Exception ex)
// {
// return -1;
// throw(ex);
// }
//}
}