Click here to Skip to main content
15,885,949 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want year break database in my C# Windows Application . After one year create new year database
with old one Database Content fields
Posted
Comments
Sergey Alexandrovich Kryukov 8-Aug-14 4:11am    
Why?!
—SA
Hemant L Patil 8-Aug-14 5:49am    
i want create March to March one Year Database
Because these application is related with account

See, for instance, here: Copy database structure but not the data[^].
 
Share this answer
 
Using Script

C#
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; }
              }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900