Пример #1
0
        /// <summary>
        /// 提供数据源连接字符串,初始化一个 <see cref="Aoite.Data.MsSqlTestManager"/> 类的新实例。
        /// </summary>
        /// <param name="connectionString">数据源连接字符串。</param>
        /// <param name="server">服务器地址。</param>
        public MsSqlTestManager(string connectionString, string server)
        {
            if(string.IsNullOrWhiteSpace(connectionString)) throw new ArgumentNullException(nameof(connectionString));
            if(string.IsNullOrWhiteSpace(server)) throw new ArgumentNullException(nameof(server));

            var provider = new SqlEngineProvider(connectionString);
            m_masterEngine = new DbEngine(provider);
            var dbDire = GA.FullPath("_databases");
            GA.IO.CreateDirectory(dbDire);
            this.DatabaseName = Guid.NewGuid().ToString();
            this.DbOwnerUser = Guid.NewGuid().ToString();
            var sql = @"CREATE DATABASE [{0}]
 ON  PRIMARY ( NAME = N'{0}', FILENAME = N'{1}\{0}.mdf' , SIZE = 5120KB , FILEGROWTH = 1024KB )
 LOG ON ( NAME = N'{0}_log', FILENAME = N'{1}\{0}_log.ldf' , SIZE = 2048KB , FILEGROWTH = 10%)";
            using(var context = m_masterEngine.Context)
            {
                context.Open();
                context
                    .Execute(sql.Fmt(this.DatabaseName, dbDire))
                    .ToNonQuery();
                this.CreateUser(this.DatabaseName, this.DbOwnerUser, context);
            }

            this.Engine = new DbEngine(new SqlEngineProvider(server, this.DatabaseName, this.DbOwnerUser, "123456"));
        }
Пример #2
0
        /// <summary>
        /// 随机的数据源路径,初始化一个 <see cref="Aoite.Data.MsCeTestManager"/> 类的新实例。
        /// </summary>
        /// <param name="databasePath">数据源路径。</param>
        public MsCeTestManager(string databasePath)
        {
            if(string.IsNullOrWhiteSpace(databasePath)) throw new ArgumentNullException(nameof(databasePath));
            this.DatabasePath = databasePath;
            GA.IO.CreateDirectory(Path.GetDirectoryName(databasePath));
            var provider = new SqlCeEngineProvider(this.DatabasePath, null);
            provider.CreateDatabase();
            var engine = new DbEngine(provider);
            this.Engine = engine;
            this.Engine.Executing += _Engine_Executing;

        }
Пример #3
0
        static void Main(string[] args)
        {
            var dbFile = GA.FullPath("Samples.db");
            var engine = new DbEngine(new SQLiteEngineProvider(dbFile, null));

            //- Test table is existed.
            if(!engine.Execute("SELECT 1 FROM sqlite_master WHERE type='table' AND name='SampleUser'").ToScalar<bool>())
            {
                //- Create SQLite table.
                engine.Execute("CREATE TABLE SampleUser(Id INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,Username nvarchar(100) NOT NULL,Password varchar(100) NOT NULL)")
                      .ToNonQuery();
            }

            //- Create db provider with SQLite.
            engine = DbEngine.Create("sqlite", "Data Source=" + dbFile + ";Version=3;");

            //- Create admin user.
            if(engine.RowCount<SampleUser>() == 0)
            {
                Console.WriteLine("Create admin(123456) user;");
                using(var context = engine.Context)
                {
                    context.Add(new SampleUser() { Username = "******", Password = "******" });
                    Console.WriteLine("The user admin Id is {0}.", context.GetLastIdentity<SampleUser>());
                }
            }

            string loginUserName = null;

            //- Create a ioc container.
            var container = new IocContainer()
                .AddService<IUserFactory>(new UserFactory(c => loginUserName))//- Get current login user callback.
                .AddService<IDbEngine>(lmps => engine.Context);//- Get or create a new current thread db context.

            var userService = new SampleUserService();
            userService.Container = container;//- Set service's container.
            do
            {
                if(loginUserName != null) Console.WriteLine($"Current user is {loginUserName}.");
                Console.Write("Please input a command:");

                var cmd = Console.ReadLine();
                switch(cmd.ToLower())
                {
                    case "login":
                        {
                            Console.Write("Please input login username:"******"Please input login password:"******"Invalid username or password.");
                                break;
                            }
                            loginUserName = username;
                            Console.WriteLine($"{loginUserName} logined.");
                        }
                        break;
                    case "logout":
                        Console.WriteLine($"{loginUserName} logouted.");
                        loginUserName = null;
                        break;
                    case "modifypassword":
                        {
                            Console.Write("Please input a new password:"******"Modify password succeed.");
                            else Console.WriteLine("Please login.");
                        }
                        break;
                    case "add":
                        {
                            Console.Write("Please input a new username:"******"Please input a new password:"******"admin") Console.WriteLine("The current user is not admin.");
                                else Console.WriteLine($"Username \"{username}\" is existed");
                                break;
                            }
                            Console.WriteLine($"{username} is added.");
                        }
                        break;
                    case "remove":
                        {
                            Console.Write("Please input the remove username:"******"admin") Console.WriteLine("The current user is not admin.");
                                else Console.WriteLine($"Username \"{username}\" is not existed");
                                break;
                            }
                            Console.WriteLine($"{username} is removed.");
                        }
                        break;
                    case "count":
                        {
                            Console.WriteLine($"{DateTime.Now.ToString("mm:ss")} : currently {userService.Count()} users.");
                        }
                        break;
                    case "list":
                        {
                            var column = string.Format("|{0,5}|{1,10}|{2,10}|", "Id", "Username", "Password");
                            Console.WriteLine();
                            Console.WriteLine(column);
                            Console.WriteLine(new string('-', column.Length));
                            var list = userService.GetList();
                            foreach(var item in list)
                            {
                                Console.WriteLine("|{0,5}|{1,10}|{2,10}|", item.Id, item.Username, item.Password);
                            }
                        }
                        break;
                    default:
                        Console.WriteLine($"Invalid command {cmd}");
                        break;
                }
                engine.ResetContext();//close db context
                Console.WriteLine();
            } while(true);

        }
Пример #4
0
 internal DbContext(DbEngine owner)
 {
     this.Id              = Guid.NewGuid();
     this.Owner           = owner;
     this._lazyConnection = new Lazy <DbConnection>(owner.Provider.CreateConnection);
 }
Пример #5
0
 internal DbContext(DbEngine owner)
 {
     this.Id = Guid.NewGuid();
     this.Owner = owner;
     this._lazyConnection = new Lazy<DbConnection>(owner.Provider.CreateConnection);
 }
Пример #6
0
 /// <summary>
 /// 设置当前运行环境的数据源查询与交互引擎的实例。
 /// </summary>
 /// <param name="engine">数据源查询与交互引擎的实例。</param>
 public static void SetEngine(DbEngine engine)
 {
     lock(SyncObjectE)
     {
         _Engine = engine;
     }
 }
Пример #7
0
 internal DbContext(DbEngine owner)
 {
     this.Id = Guid.NewGuid();
     this.Owner = owner;
     this._connection = owner.Provider.CreateConnection();
 }
Пример #8
0
        /// <summary>
        /// 执行与释放或重置托管资源相关的应用程序定义的任务。
        /// </summary>
        protected override void DisposeManaged()
        {
            using(var context = this.m_masterEngine.Context)
            {
                context.Open();
                context.Connection.ChangeDatabase("master");
                context.Execute("ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE".Fmt(this.DatabaseName)).ToNonQuery();
                context.Execute("DROP DATABASE [{0}]".Fmt(this.DatabaseName)).ToNonQuery();
                context.Execute("DROP LOGIN [{0}]".Fmt(this.DbOwnerUser)).ToNonQuery();
            }
            this.m_masterEngine = null;

            base.DisposeManaged();
        }