26
2017
09

Photon_使用NHibernate向数据库中增删改查数据_006

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;
using NHibernate.Cfg;
using YYEdu.Model;
namespace YYEdu {
    class Program {
        static void Main(string[] args)
        {
            var configueration = new Configuration();
            //解析hibernate.cfg.xml文件
            configueration.Configure();
            //解析映射文件
            configueration.AddAssembly("YYEdu");
            ISessionFactory sessionFactory = null;
            try
            {
                sessionFactory = configueration.BuildSessionFactory();
                ISession session = sessionFactory.OpenSession();

                //插入操作
                //User user = new User() { Username = "yy01", Password = "123456" };
                //session.Save(user);
                //session.Flush();

                //修改操作
                //User obj = session.Get<User>(16);
                //obj.Password = "123456";
                //session.Update(obj);
                //session.Flush();

                //删除操作
                //User obj1 = session.Get<User>(16);
                //session.Delete(obj1);
                //session.Flush();

                //查询
                ISQLQuery query = session.CreateSQLQuery("select * from users").AddEntity(typeof(User));
                IList<User> u = query.List<User>();
                Console.WriteLine(u[0].Username);
                Console.WriteLine();

            }
            catch (Exception e)
            {

                Console.WriteLine(e);
            }
            finally
            {
                if (sessionFactory != null)
                {
                    sessionFactory.Close();
                }
            }

        }
    }
}
上一篇:Photon_使用ExecuteReader读取数据库中的数据_003 下一篇:工厂设计模式 - 数据存储的特有方式