我有这个代码,我有这个问题
File Compte.cs
public bool SaveUser(string identification, string acc, string mot, string notify, string nom, string phone, string mail) {
try
{
if (identification == null || acc == null || nom == null || mail == null || mot == null) return false;
ITransaction transaction = User.OpenSession().BeginTransaction();
User u = new User() { Account = acc, Identification = identification, ContactEmail = mail, ContactName = nom, ContactPhone = phone, NotifyEmail = notify, Password = mot };
User.OpenSession().SaveOrUpdate(u);
transaction.Commit();
ITransaction transaction2 = User.OpenSession().BeginTransaction();
Useracl ua = new Useracl { Account = acc, UserID = identification, AccessLevel = 1, AclID = (Useracl.GetUseracl().Count + 1).ToString() };
Useracl.OpenSession().SaveOrUpdate(ua);
transaction2.Commit();
return true;
}
catch { return false; }
}
文件管理.s
public ActionResult Index()
{
ViewBag.Title = c.GetUserID().Count.ToString();
return View();
}
public ActionResult BeforeRegister()
{
return View();
}
public ActionResult AfterRegister(string Pseudo, string Phone, string Email, string Password, string Notify)
{
bool a = c.SaveUser((c.GetPassword().Count + 1).ToString(), (c.GetPassword().Count + 1).ToString(), Password, Notify, Pseudo, Phone, Email);
if (a)
{
return RedirectToAction("Index", "Administration");
}
else
return RedirectToAction("BeforeRegister", "Administration");
}
答案 0 :(得分:1)
首先,您可以使用if (!String.IsNullOrEmpty(myString))
而不是if (myString==null)
。
此外,您可能希望在using
块中使用您的会话。
bool ret= new bool();
if ((!String.IsNullOrEmpty(foo1)) && (!String.IsNullOrEmpty(foo2)))
{
//ConnectionDB is a public class, with a static method ISessionFactory SessionFactory(), and the method OpenSession() returns an ISession
using (NHibernate.ISession nhSession = ConnectionDB.SessionFactory.OpenSession())
{
try
{
User u = new User();
//set your User
nhSession.Transaction.Begin();
nhSession.Save(u);
nhSession.Transaction.Commit();
nhSession.Close();
ret = true;
}
catch (Exception ex)
{
ret = false;
}
}
return ret;
现在,关于不插入的查询,它可能是一个映射问题,或者你可能在其他地方压制异常(比如你的静态方法User.OpenSession().SaveOrUpdate(u)
)