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();
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();
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; }
}