如何创建新年数据库 [英] How to create new year database

查看:49
本文介绍了如何创建新年数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C#Windows应用程序中使用年度数据库。一年后用旧的数据库内容字段创建新年数据库

I want year break database in my C# Windows Application . After one year create new year database
with old one Database Content fields

推荐答案

例如,请参见:复制数据库结构但不是数据 [ ^ ]。
See, for instance, here: Copy database structure but not the data[^].


使用脚本



Using Script

if (date >= 1 && mth >= 4)
          {
              fnm = f + "_" + yr + (yr + 1);

              if (!cfd.IsExists(fnm))
              {
                  DialogResult dr = MessageBox.Show("Create New Database", "CONFIRMATION", MessageBoxButtons.YesNo);
                  if (dr == DialogResult.Yes)
                  {
             SqlConnection contemp = new SqlConnection();
                contemp.ConnectionString = "Data Source=" + ServerName + ";Initial Catalog=master;User ID=sa;Password=Ultimate123;Integrated Security=false";
                contemp.Open();
                SqlCommand cmdtemp = new SqlCommand();
                cmdtemp.Connection = contemp;
                cmdtemp.CommandType = CommandType.Text;
                cmdtemp.CommandText = "SELECT * FROM master.dbo.sysdatabases WHERE Name='" + dbname + "'";
                SqlDataReader dr1 = cmdtemp.ExecuteReader();
                string script;
                if (dr1.HasRows)
                {
                    dr1.Close();
                    cmdtemp.CommandText = "DROP DATABASE " + dbname;
                    cmdtemp.ExecuteNonQuery();
                }
                else dr1.Close();

                //create database with dbname
                script = File.ReadAllText(Application.StartupPath + "\\" + "dbcashflowcreator.sql");
                script = script.Replace("GOTO", "xxxx");
                script = script.Replace("GO", "");
                script = script.Replace("xxxx", "GOTO");
                script = script.Replace("dbcashflow", dbname);
                cmdtemp.CommandText = script;
                cmdtemp.ExecuteNonQuery();

                //create database objects
                script = File.ReadAllText(Application.StartupPath + "\\" + "DBObjectCreater.sql");
                script = script.Replace("GOTO", "xxxx");
                script = script.Replace("GO", "");
                script = script.Replace("xxxx", "GOTO");
                script = script.Replace("dbcashflow", dbname);
                cmdtemp.CommandText = script;
                cmdtemp.ExecuteNonQuery();
                  }
                  if (dr == DialogResult.No) { return; }
              }


这篇关于如何创建新年数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆