Exemplo n.º 1
0
        /// <summary>
        /// 根据指定的数据库类型创建一个Service供使用
        /// 即建即用,不与ModelBase的生命周期一致
        /// </summary>
        /// <returns></returns>
        private SQLService GetSQLService()
        {
            SQLService        service = null;
            DataBaseTypesEnum DbType  = DataBaseTypesEnum.Unknow;

            //根据枚举的附加属性获取对应的数据库类型服务并创建对象
            if (dataBaseType.Value != DataBaseTypesEnum.Unknow || defaultDbType != DataBaseTypesEnum.Unknow)
            {
                // if not set type in this thread then use the default setting
                if (dataBaseType.Value == DataBaseTypesEnum.Unknow)
                {
                    dataBaseType.Value = defaultDbType;
                }
                DbType = dataBaseType.Value;

                var fieldName = Enum.GetName(typeof(DataBaseTypesEnum), DbType);
                var enumAttr  = typeof(DataBaseTypesEnum).GetField(fieldName).GetCustomAttribute(typeof(EnumHelperAttribute)) as EnumHelperAttribute;
                //var enumAttr = attrb as EnumHelperAttribute;
                string serviceName = "MyORM.DbService." + enumAttr.Describe;
                var    serviceType = Type.GetType(serviceName);
                if (serviceType == null)
                {
                    throw new ArgumentException("未创建或路径错误,导致无法获取到数据库服务类:" + serviceName);
                }
                service = Activator.CreateInstance(serviceType) as SQLService;
            }
            else
            {
                throw new ArgumentException("未指定数据库类型!");
            }
            return(service);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            string exePath          = @"F:\C_shap\Git\MyORM\trunk\TestApp";
            string mySQL_conStr     = "Your MySQL connection string";
            string sqlServer_conStr = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + exePath + @"\TestDataBases\SQLServerDB.mdf" + ";Integrated Security=True;Connect Timeout=30";
            string sqlite_conStr    = @"Data Source=" + exePath + @"\TestDataBases\SQLiteDB.db";

            // step 1  SetConnectionString
            MySQLService.SetConnection(mySQL_conStr);
            SQLServerService.SetConnection(sqlServer_conStr);
            SQLiteService.SetConnection(sqlite_conStr);

            DataBaseTypesEnum DBType  = DataBaseTypesEnum.SQLite;
            SQLService        service = null;

            switch (DBType)
            {
            case DataBaseTypesEnum.MySQL:
            {
                //step 2 tell Service that which type of database you want to use in default
                ModelBase.SetDefaultService(DataBaseTypesEnum.MySQL);

                //step 3 create a new instance and use it
                service = new MySQLService();
                break;
            }

            case DataBaseTypesEnum.SQLServer:
            {
                ModelBase.SetDefaultService(DataBaseTypesEnum.SQLServer);
                service = new SQLServerService();
                break;
            }

            case DataBaseTypesEnum.SQLite:
            {
                ModelBase.SetDefaultService(DataBaseTypesEnum.SQLite);
                service = new SQLiteService();
                break;
            }
            }

            LoadById(service);

            ReadAll(service);

            ReadByCondition(service);

            ReadPageList(service);

            Update(service);

            Insert(service);

            Delete(service);

            Console.ReadKey();
        }
Exemplo n.º 3
0
 /// <summary>
 /// Change Database type in current thread,other threads have no effect
 /// </summary>
 /// <param name="DbType">Database Type</param>
 public static void ChangeService(DataBaseTypesEnum DbType)
 {
     dataBaseType.Value = DbType;
 }
Exemplo n.º 4
0
 /// <summary>
 /// Set the default Database Type in this application, all threads will auto use this setting unless call ChangeService()
 /// </summary>
 /// <param name="DbType"></param>
 public static void SetDefaultService(DataBaseTypesEnum DbType)
 {
     defaultDbType = DbType;
 }