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保存的速度有点慢。。。祝大家新年筷乐。。。。。。。。。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值