habib ullah

habib ullah

  • NA
  • 229
  • 14.3k

the connection string property has not been initalize

Jun 1 2017 8:35 AM
private void btn_browse_Click(object sender, EventArgs e)
{
FolderBrowserDialog dl = new FolderBrowserDialog();
if (dl.ShowDialog() == DialogResult.OK)
{
tbx_bacup.Text = dl.SelectedPath;
btn_buckup.Enabled = true;
}
}
private void btn_buckup_Click(object sender, EventArgs e)
{
string database = con.Database.ToString();
try
{
if (tbx_bacup.Text == string.Empty)
{
MessageBox.Show("please inter backup file location");
}
else
{
string cmd = "BACKUP DATABASE [" + database + "] TO DISK ='" + tbx_bacup.Text + "\\" + " DATABASE " + "-" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".back'";
using (SqlCommand command = new SqlCommand())
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
command.ExecuteNonQuery();
con.Close();
MessageBox.Show("Database backup succefily ");
btn_buckup.Enabled = true;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btn_restoreee_Click(object sender, EventArgs e)
{
OpenFileDialog pn = new OpenFileDialog();
pn.Filter = "SQL SERVER database backup files |*.bak";
pn.Title = " Database restore ";
if (pn.ShowDialog() == DialogResult.OK)
{
tbx_restore.Text = pn.FileName;
btn_restore.Enabled = true;
}
}
private void btn_restore_Click(object sender, EventArgs e)
{
string database = con.Database.ToString();
try
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
string sql = string.Format("ALTER DATABASE[" + database + "] SET SINGLE_USER WITH ROOLLBACK IMMEDIATE");
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
string sqll = string.Format("USE MASTER RESTORE DATABASE [" + database + "] FROM DISK ='" + tbx_restore.Text + "' WITH REPLACE;");
SqlCommand cmdd = new SqlCommand(sqll, con);
cmd.ExecuteNonQuery();
string sqlll = string.Format("ALTER DATABASE [" + database + "] SET MULTI_USER");
SqlCommand cmddd = new SqlCommand(sqlll, con);
cmd.ExecuteNonQuery();
MessageBox.Show("database restoration succefily");
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

Answers (5)