using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 数据库.DB
{
class UserManage
{
public static UserManage Instance = new UserManage();
///
/// 通过账号和密码查看用户是否存在
///
/// 账号
/// 密码
///
public User Login(string uName, string psw)
{
using (ISession session = NHibernateHelper.OpenSession())
{
using (ITransaction ita = session.BeginTransaction())
{
var iq = session.QueryOver().Where(user => user.account == uName && user.password == psw);
IList res = iq.List();
if (res.Count > 0)
{
return res[0];
}
}
}
return null;
}
///
/// 通过账号和密码添加新的用户
///
/// 账号
/// 密码
///
public bool add(string account, string password)
{
bool flag = false;
using (ISession session = NHibernateHelper.OpenSession())
{
using (ITransaction ita = session.BeginTransaction())
{
User user = new User();
user.account = account;
user.password = password;
Object obj = session.Save(user);
ita.Commit();
if (obj != null)
{
flag = true;
}
}
}
return flag;
}
///
/// 通过账号删除用户
///
/// 账号
/// 密码
///
public bool Delete(string account, string password)
{
bool flag = false;
using (ISession session = NHibernateHelper.OpenSession())
{
using (ITransaction ita = session.BeginTransaction())
{
try
{
session.Delete("From User Where account='" + account + "' and password");
ita.Commit();
flag = true;
}
catch (Exception)
{
flag = false;
}
}
}
return flag;
}
///
/// 通过账号和旧的密码更新新的密码
///
/// 账号
/// 旧的密码
/// 新的密码
///
public bool Updata(string oldAccount, string oldPassword,string newPassword)
{
bool flag = false;
using (ISession session = NHibernateHelper.OpenSession())
{
using (ITransaction ita = session.BeginTransaction())
{
try
{
string sql = "UPDATE user SET password='" + newPassword + "' WHERE (account='"+oldAccount+"' and password='"+oldPassword+"')";
ISQLQuery query = session.CreateSQLQuery(sql);
int ret=query.ExecuteUpdate();
Console.WriteLine("updata res="+ret);
ita.Commit();
flag = true;
}
catch (Exception)
{
flag = false;
}
}
}
return flag;
}
}
}