关闭

EF5批量操作数据库(批量创建、删除、附加、分离、脱机、联机)

719人阅读 评论(0) 收藏 举报
分类:

工作中涉及到一大批数据库的操作,特意写了以下demo程序备忘


<span style="white-space:pre">	</span>const int DbNumber = 5;
        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var entity = new LocalDBEntities();
                var path = this.txtPath.Text;
                for (var i = 1; i <= DbNumber; i++)
                {
                    var command = string.Format("use master create database {0} on primary (name='{0}',filename='{1}\\{0}.mdf',size=5MB,maxsize=10MB,filegrowth=1MB) log on (name='{0}Log',filename='{1}\\{0}.ldf',size=2MB,maxsize=20MB,filegrowth=1MB)", "test" + i, path);
                    entity.Database.ExecuteSqlCommand(command);
                }

                MessageBox.Show("创建成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnOffline_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var entity = new LocalDBEntities();
                var path = this.txtPath.Text;
                for (var i = 1; i <= DbNumber; i++)
                {
                    var command = string.Format("use master exec sp_dboption '{0}','offline','true'", "test" + i);
                    entity.Database.ExecuteSqlCommand(command);
                }

                MessageBox.Show("脱机成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnOnline_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var entity = new LocalDBEntities();
                var path = this.txtPath.Text;
                for (var i = 1; i <= DbNumber; i++)
                {
                    var command = string.Format("use master exec sp_dboption '{0}','offline','false'", "test" + i);
                    entity.Database.ExecuteSqlCommand(command);
                }

                MessageBox.Show("联机成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAttach_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var entity = new LocalDBEntities();
                var path = this.txtPath.Text;
                for (var i = 1; i <= DbNumber; i++)
                {
                    var dbname = "test" + i;
                    var command = string.Format("use master exec sp_attach_db @dbname='{0}',@filename1='{1}',@filename2='{2}'"
                        , dbname
                        , path + "\\" + dbname + ".mdf"
                        , path + "\\" + dbname + ".ldf");
                    entity.Database.ExecuteSqlCommand(command);
                }

                MessageBox.Show("附加成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDettach_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var entity = new LocalDBEntities();
                var path = this.txtPath.Text;
                for (var i = 1; i <= DbNumber; i++)
                {
                    var command = string.Format("use master exec sp_detach_db '{0}'", "test" + i);
                    entity.Database.ExecuteSqlCommand(command);
                }

                MessageBox.Show("分离成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDrop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var entity = new LocalDBEntities();
                var path = this.txtPath.Text;
                for (var i = 1; i <= DbNumber; i++)
                {
                    var command = string.Format("use master drop database {0}", "test" + i);
                    entity.Database.ExecuteSqlCommand(command);
                }

                MessageBox.Show("删除成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:78774次
    • 积分:1124
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:80篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论