123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- using FluentNHibernate.Cfg;
- using FluentNHibernate.Mapping;
- using NHibernate;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace ProjectManagementSystem.NHibernateDBHelper
- {
- class WMSDBModelHelper
- {
- static private volatile ISessionFactory m_factory = null;
- static private object obj = new object();
- static private string _connectionString;
- static public ISession Session
- {
- get
- {
- if (m_factory == null)
- {
- lock (obj)
- {
- try
- {
- if (m_factory == null) m_factory = createSessionFactory();
- }
- catch { throw; }
- }
- }
- return m_factory.OpenSession();
- }
- }
- static public void initialize(string path)
- {
- _connectionString = "Data Source =" + path;
- try
- {
- if (m_factory != null) m_factory.Dispose();
- m_factory = createSessionFactory();
- }
- catch (Exception ex) { Console.Write(ex.ToString()); throw; }
- }
- public static void initialize(string strDatabaseServer,
- int databasePort,
- string strDatabaseName,
- string strUserName,
- string strPassword)
- {
- //if (m_sessionFactory != null) return;
- try
- {
- string connectionString = string.Format("Server={0};Port={1};Database={2};Uid={3};Pwd={4};",
- strDatabaseServer, databasePort, strDatabaseName, strUserName, strPassword);
- FluentConfiguration config = Fluently.Configure()
- .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
- .ConnectionString(connectionString));
- Assembly curAssembly = Assembly.GetExecutingAssembly();
- NHibernate.Cfg.Configuration cfg = config.Mappings(m => m.FluentMappings.AddFromAssembly(curAssembly)).BuildConfiguration();
- m_factory = cfg.BuildSessionFactory();
- // m_mappingSet.buildMappings(cfg);
- }
- catch { throw; }
- }
- internal static void InitWareHousesStatus()
- {
- using (var session = Session)
- {
- using (var transaction = session.BeginTransaction())
- {
- try
- {
- ISQLQuery qry1 = session.CreateSQLQuery("update t_wh_locationProperty set Status='2'");
- qry1.ExecuteUpdate();
- //ISQLQuery qry2 = session.CreateSQLQuery("delete from OperationRecord");
- //qry2.ExecuteUpdate();
- //ISQLQuery qry3 = session.CreateSQLQuery("delete from DemandStation");
- //qry3.ExecuteUpdate();
- //ISQLQuery qry4 = session.CreateSQLQuery("delete from Station");
- //qry4.ExecuteUpdate();
- //ISQLQuery qry5 = session.CreateSQLQuery("delete from NavigatePath");
- //qry5.ExecuteUpdate();
- //ISQLQuery qry6 = session.CreateSQLQuery("delete from NavigateNode");
- //qry6.ExecuteUpdate();
- }
- catch (Exception ex)
- {
- transaction.Rollback();
- throw ex;
- }
- }
- }
- }
- static public void close()
- {
- if (m_factory != null) { m_factory.Close(); m_factory = null; }
- }
- static private ISessionFactory createSessionFactory()
- {
- try
- {
- string connectionString = _connectionString;
- FluentConfiguration config = Fluently.Configure()
- .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
- .ConnectionString(connectionString));
- Assembly curAssembly = Assembly.GetExecutingAssembly();
- NHibernate.Cfg.Configuration cfg = config.Mappings(m => m.FluentMappings.AddFromAssembly(curAssembly))
- .ExposeConfiguration(f => f.SetInterceptor(new SqlStatementInterceptor()))
- .BuildConfiguration();
- return cfg.BuildSessionFactory();
- }
- catch { throw; }
- }
- public class SqlStatementInterceptor : EmptyInterceptor
- {
- public override NHibernate.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql)
- {
- //#if DEBUG
- try
- {
- //File.AppendAllText("c:\\log.txt", string.Format("{0}\n", sql.ToString()));
- }
- catch
- {
- }
- return sql;
- }
- }
- }
- #region WareHouseLocation
- public class WareHouseLocation
- {
- private string _WareHouseCode;
- private string _LocationCode;
- private string _Area;
- private string _Status;
- private string _LocationName;
- //private string _WareHouseType;
- //private string _CallCount;
- private string _Material;
- private string _Time;
- private string _Column;
- private string _Row;
- //private int _Mark;
- public virtual string LocationCode
- {
- get
- {
- return _LocationCode;
- }
- set
- {
- _LocationCode = value;
- }
- }
- public virtual string Attribute1
- {
- get
- {
- return _LocationName;
- }
- set
- {
- _LocationName = value;
- }
- }
- public virtual string Attribute2
- {
- get
- {
- return _Column;
- }
- set
- {
- _Column = value;
- }
- }
- public virtual string Attribute3
- {
- get
- {
- return _Row;
- }
- set
- {
- _Row = value;
- }
- }
- public virtual string Attribute4
- {
- get
- {
- return _Material;
- }
- set
- {
- _Material = value;
- }
- }
- public virtual string Attribute5
- {
- get
- {
- return _Time;
- }
- set
- {
- _Time = value;
- }
- }
- public virtual string Area
- {
- get
- {
- return _Area;
- }
- set
- {
- _Area = value;
- }
- }
- public virtual string WareHouseCode
- {
- get
- {
- return _WareHouseCode;
- }
- set
- {
- _WareHouseCode = value;
- }
- }
- public virtual string Status
- {
- get
- {
- return _Status;
- }
- set
- {
- _Status = value;
- }
- }
- //public virtual int Mark
- //{
- // get
- // {
- // return _Mark;
- // }
- // set
- // {
- // _Mark = value;
- // }
- //}
- }
- class MappingWareHouseLocation : ClassMap<WareHouseLocation>
- {
- public MappingWareHouseLocation()
- {
- Table("t_wh_locationProperty");
- Id(m => m.LocationCode).GeneratedBy.Assigned();
- Map(m => m.WareHouseCode);//.Increment();
- Map(m => m.Status);
- Map(m => m.Area);
- Map(m => m.Attribute1);
- Map(m => m.Attribute2);
- Map(m => m.Attribute3);
- Map(m => m.Attribute4);
- Map(m => m.Attribute5);
- //Map(m => m.Mark);
- }
- }
- #endregion
- }
|