c#使用FluentNHibernate,多數據庫連接,一個程序,連接多個數據庫

直接貼代碼,代碼簡單易懂,不懂的留言

代理接口類

 public interface IRepository<T>
    {
        bool Save(T entity, DBs db);
        bool Update(T entity, DBs db);
        bool Delete(T entity, DBs db);
        bool SaveOrUpdate(T entity, DBs db);
    }

數據庫枚舉類

public enum DBs
    {
        Student,//數據庫1
        Student2,//數據庫2
    }

實現代理接口

 public class NHibernateRepository<T> : IRepository<T> where T : class
    {
        protected static Configuration config; 

//數據庫連接,你懂得
        private static string strlink =
                "Data Source=127.0.0.1;Initial Catalog=Student;Integrated Security=False;Persist Security Info=True;User ID=sa;Password=1";
        string strlink2 =
               "Data Source=127.0.0.1;Initial Catalog=Student2;Integrated Security=False;Persist Security Info=True;User ID=sa;Password=1";
        protected static IDictionary<string, ISessionFactory> _allFactories;
//初始化多個數據庫

        public NHibernateRepository()
        {
            IDictionary<string, string> dataBases = new Dictionary<string, string>();
            dataBases.Add("Student", strlink);
            dataBases.Add("Student2", strlink2); 
            _allFactories = new Dictionary<string, ISessionFactory>(dataBases.Count);
            foreach (var dataBase in dataBases)
            {
                config = Fluently.Configure()
                    .Database(
                        MsSqlConfiguration.MsSql2008.ConnectionString(dataBase.Value))
                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<NHibernateRepository<T>>())
                    .BuildConfiguration();
                _allFactories.Add(dataBase.Key, config.BuildSessionFactory());
            }
        }


        public ISession GetSession1()
        {
            return _allFactories["Student"].OpenSession();
        }
        public ISession GetSession2()
        { 
            return _allFactories["Student2"].OpenSession();
        }




        public bool Save(T entity, DBs db)
        {
            bool isOk = false;
            if (db == DBs.Student)
            {
                using (var s = GetSession1())
                {
                    try
                    {
                        s.Save(entity);
                        s.Flush();
                        isOk = true;
                    }
                    catch (Exception exception)
                    {
                        isOk = false;
                    }
                }
            }
            else
            {
                using (var s = GetSession2())
                {
                    try
                    {
                        s.Save(entity);
                        s.Flush();
                        isOk = true;
                    }
                    catch (Exception exception)
                    {
                        isOk = false;
                    }


                }
            }
            return isOk;
        }


        public bool Update(T entity, DBs db)
        {
            bool isOk = false;
            if (db == DBs.Student)
            {
                using (var s = GetSession1())
                {
                    try
                    {
                        s.Update(entity);
                        s.Flush();
                        isOk = true;
                    }
                    catch (Exception exception)
                    {
                        isOk = false;
                    }
                }
            }
            else
            {
                using (var s = GetSession2())
                {
                    try
                    {
                        s.Update(entity);
                        s.Flush();
                        isOk = true;
                    }
                    catch (Exception exception)
                    {
                        isOk = false;
                    }


                }
            }
            return isOk;
        }


        public bool Delete(T entity, DBs db)
        {
            bool isOk = false;
            if (db == DBs.Student)
            {
                using (var s = GetSession1())
                {
                    try
                    {
                        s.Delete(entity);
                        s.Flush();
                        isOk = true;
                    }
                    catch (Exception exception)
                    {
                        isOk = false;
                    }
                }
            }
            else
            {
                using (var s = GetSession2())
                {
                    try
                    {
                        s.Delete(entity);
                        s.Flush();
                        isOk = true;
                    }
                    catch (Exception exception)
                    {
                        isOk = false;
                    }


                }
            }
            return isOk;
        }


        public bool SaveOrUpdate(T entity, DBs db)
        {
            bool isOk = false;
            if (db == DBs.Student)
            {
                using (var s = GetSession1())
                {
                    try
                    {
                        s.SaveOrUpdate(entity);
                        s.Flush();
                        isOk = true;
                    }
                    catch (Exception exception)
                    {
                        isOk = false;
                    }
                }
            }
            else
            {
                using (var s = GetSession2())
                {
                    try
                    {
                        s.SaveOrUpdate(entity);
                        s.Flush();
                        isOk = true;
                    }
                    catch (Exception exception)
                    {
                        isOk = false;
                    }


                }
            }
            return isOk;
        }

    }

如何使用呢,請看下面

例如:

  NHibernateRepository<Vip> repository = new NHibernateRepository<Vip>();

            IList<Vip> list =
                repository.GetSession1()
                    .QueryOver<Vip>()
                    .Where(x => x.CODE != " ")
                    .OrderBy(x => x.CODE)
                    .Desc.List<Vip>();//從第一個數據庫取數據

 for (int i = 0; i < list.Count; i++)
            {
             
                Vip vip = list[i];
                bool isok = false;
                isok = repository.Save(vip, DBs.Student2);//保存到第二個數據庫
                if (!isok)
                {
                    vip.NAME = vip.NAME + "(修改)";
                    isok = repository.Update(vip, DBs.Student2);//修改到第二個數據庫
                    if (isok)
                    {
                        Console.Write("ok");
                    }
                }
            }

爲什麼不用SaveOrUpdate呢,因爲SaveOrUpdate保存的速度有點慢。。。祝大家新年筷樂。。。。。。。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章