Example #1
0
    static void Main(string[] args)
    {
        String connString = @"
        Data Source=.;
        Initial Catalog=Samochody;
        Integrated Security=True
        ";

        DataContext      sDataContext = new DataContext(connString);
        Table <Samochod> Samochody    = sDataContext.GetTable <Samochod>();

        if (sDataContext.DatabaseExists())
        {
            Console.WriteLine("Deleting old database...");
            sDataContext.DeleteDatabase();
        }
        sDataContext.CreateDatabase();

        // Insert
        Samochod s = new Samochod()
        {
            ID = 1, IDMarka = 1, Kolor = "Czarny"
        };

        Samochody.InsertOnSubmit(s);
        sDataContext.SubmitChanges();
    }
Example #2
0
        protected override void CreateDatabase()
        {
            IConfiguration config    = new BasicConfiguration();
            var            container = config.Container;
            DataContext    context   = container.Resolve <DataContext>();

            if (context.DatabaseExists())
            {
                context.DeleteDatabase();
            }
            context.CreateDatabase();
        }
Example #3
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));
        }
Example #4
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));
        }
Example #5
0
 /// <summary>
 /// 删除数据库重新创建
 /// </summary>
 /// <param name="context"></param>
 public static void Delete(DataContext context)
 {
     context.DeleteDatabase();
     Create(context);
 }
Example #6
0
 /// <summary>
 /// 删除数据库重新创建
 /// </summary>
 /// <param name="context"></param>
 public static void Delete(DataContext context)
 {
     context.DeleteDatabase();
     Create(context);
 }