private void CopyDb(StandardKernel kernel, out FileInfo sandboxFile, out string connectionString)
        {
            var config = kernel.Get<IConfig>();
            var db = new DataContext(config.ConnectionStrings("ConnectionString"));

            TestDbName = string.Format("{0}_{1}", NameDb, DateTime.Now.ToString("yyyyMMdd_HHmmss"));
            sandboxFile = new FileInfo(string.Format("{0}\\{1}.bak", Sandbox, TestDbName));
            var sandboxDir = new DirectoryInfo(Sandbox);

            //backupFile
            var textBackUp = string.Format(@"-- Backup the database
            BACKUP DATABASE [{0}]
            TO DISK = '{1}'
            WITH COPY_ONLY",
            NameDb, sandboxFile.FullName);
            db.ExecuteCommand(textBackUp);

            var restoreFileList = string.Format("RESTORE FILELISTONLY FROM DISK = '{0}'", sandboxFile.FullName);
            var fileListRestores = db.ExecuteQuery<FileListRestore>(restoreFileList).ToList();
            var logicalDbName = fileListRestores.FirstOrDefault(p => p.Type == "D");
            var logicalLogDbName = fileListRestores.FirstOrDefault(p => p.Type == "L");

            var restoreDb = string.Format("RESTORE DATABASE [{0}] FROM DISK = '{1}' WITH FILE = 1, MOVE N'{2}' TO N'{4}\\{0}.mdf', MOVE N'{3}' TO N'{4}\\{0}.ldf', NOUNLOAD, STATS = 10", TestDbName, sandboxFile.FullName, logicalDbName.LogicalName, logicalLogDbName.LogicalName, sandboxDir.FullName);
            db.ExecuteCommand(restoreDb);

            connectionString = config.ConnectionStrings("ConnectionString").Replace(NameDb, TestDbName);
        }
		private static void ClearTpDB(DataContext tpDatabaseDataContext)
		{
            if (!tpDatabaseDataContext.DatabaseExists())
            {
                tpDatabaseDataContext.CreateDatabase();
                tpDatabaseDataContext.SubmitChanges();
            }
            else
            {
                tpDatabaseDataContext.ExecuteCommand("delete from Project");
                tpDatabaseDataContext.ExecuteCommand("delete from TpUser");
                tpDatabaseDataContext.ExecuteCommand("delete from General");
                tpDatabaseDataContext.ExecuteCommand("delete from MessageUid");
                tpDatabaseDataContext.ExecuteCommand("delete from PluginProfile");
                tpDatabaseDataContext.ExecuteCommand("delete from Revision");  
            }
		}
		private static void ClearTpDB(DataContext tpDatabaseDataContext)
		{
			tpDatabaseDataContext.ExecuteCommand("delete from Project");
			tpDatabaseDataContext.ExecuteCommand("delete from CustomReport");
			tpDatabaseDataContext.ExecuteCommand("delete from TpUser");
			tpDatabaseDataContext.ExecuteCommand("delete from General");
			tpDatabaseDataContext.ExecuteCommand("delete from MessageUid");
			tpDatabaseDataContext.ExecuteCommand("delete from PluginProfile");
			tpDatabaseDataContext.ExecuteCommand("delete from Revision");
		}
Example #4
0
        public Form1()
        {
            InitializeComponent();

            string connString = "DataSource=db/Pots.db3;";
            SQLiteConnection connection = new SQLiteConnection(connString);
            DataContext = new DataContext(connection);
            DataContext.ExecuteCommand("PRAGMA foreign_keys=ON;");
            formEditor = new FormEditor();
            diagramView1.OnItemSelect += (obj, index) =>
                {
                    propertyGrid1.SelectedObject = obj;
                };
            buttonRefresh_Click(null, null);
        }
Example #5
0
        protected void ReCreateDatabaseFromAttributeMapping(DataContext ctx)
        {
            bool success = false;
            bool retry = false;
            int retries = 0;
            do
            {
                try
                {
                    using (ctx)
                    {
                        CloseAllOpenConnections(ctx);
                        ctx.Log = new StringWriter();

                        if (ctx.DatabaseExists())
                        {
                            //drop all connections by disabling multi-user (and immediatly abort all current transactions)
                            ctx.ExecuteCommand("ALTER DATABASE " + _databaseName + " SET SINGLE_USER WITH ROLLBACK IMMEDIATE");
                            ctx.DeleteDatabase();
                        }
                        try
                        {
                            ctx.CreateDatabase();
                        }
                        catch (Exception e)
                        {
                            Debug.WriteLine(e);
                            Debug.WriteLine(ctx.Log);
                            throw;
                        }

                        //re-enable multi-user (again)
                        ctx.ExecuteCommand("ALTER DATABASE " + _databaseName + " SET MULTI_USER");
                        success = true;
                    }
                }
                catch (SqlException e)
                {
                    retry = false;
                    if (e.Message.IndexOf("was deadlocked on lock resources with another process and has been chosen as the deadlock victim") > 0)
                    {
                        retry = true;
                        retries++;
                    }
                }
            } while (success == false && (retry && retries < 3));
        }
        public void PutMessage(ImmutableEnvelope envelope)
        {
            var buffer = _streamer.SaveEnvelopeData(envelope);
            var now = DateTime.UtcNow;

            using (var db = new DataContext(_connectionString))
            {
                db.ExecuteCommand
                (
                    "exec [dbo].[Enqueue] {0}, {1}, {2}, {3}, {4}",
                    _queueID,
                    envelope.EnvelopeId,
                    envelope.CreatedOnUtc,
                    envelope.DeliverOnUtc < now ? now : envelope.DeliverOnUtc,
                    buffer
                );
            }
        }
 void SendToPoisonQueue(QueueMessage message)
 {
     using (var db = new DataContext(_config.ConnectionString))
     {
         db.ExecuteCommand
         (
             "exec [dbo].[Enqueue] {0}, {1}, {2}, {3}, {4}",
             _poisonQueueID,
             message.EnvelopeID,
             message.CreatedOnUtc,
             DateTime.UtcNow,
             message.Envelope
         );
     }
 }
 void DeleteMessage(QueueMessage message)
 {
     using (var db = new DataContext(_config.ConnectionString))
     {
         db.ExecuteCommand
         (
             "exec [dbo].[Delete] {0}",
             message.MessageID
         );
     }
 }
        private void RemoveDb()
        {
            var config =  DependencyResolver.Current.GetService<IConfig>();

            var db = new DataContext(config.ConnectionStrings("ConnectionString"));

            var textCloseConnectionTestDb = string.Format(@"ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", TestDbName);
            db.ExecuteCommand(textCloseConnectionTestDb);

            var textDropTestDb = string.Format(@"DROP DATABASE [{0}]", TestDbName);
            db.ExecuteCommand(textDropTestDb);
        }
Example #10
0
        /// <summary>  
        /// 创建单个表  
        /// </summary>  
        /// <param name="linqTableClass"></param>
        /// <param name="db"></param>  
        private static void CreateTable(Type linqTableClass, DataContext db)
        {
            var metaTable = db.Mapping.GetTable(linqTableClass);
            //反射的方法创建对象
            var typeName = "System.Data.Linq.SqlClient.SqlBuilder";
            var type = typeof(DataContext).Assembly.GetType(typeName);

            //反射调用私有的GetCreateTableCommand 方法。
            var bf = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod;
            var sql = type.InvokeMember("GetCreateTableCommand", bf, null, null, new[] { metaTable });

            var sqlAsString = sql.ToString();
            db.ExecuteCommand(sqlAsString);
        }