Monday, April 19, 2010

Restore a Data Base from C#

 private void btnRestore_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                if (File.Exists(txtPath.Text + txtDireccion.Text + ".bak"))
                {
                    if (MessageBox.Show("¿Está seguro de restaurar?", "Respaldo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        if (Program.laconexion.State != System.Data.ConnectionState.Open)
                            Program.laconexion.Open();

                        SqlCommand command = new SqlCommand("use master", Program.laconexion);
                        command.ExecuteNonQuery();
                        command = new SqlCommand(@"restore database FABRINOX from disk ='"+txtPath.Text + txtDireccion.Text + ".bak'", Program.laconexion);
                        command.ExecuteNonQuery();
                        Program.laconexion.Close();

                        MessageBox.Show("Se ha restaurado la base de datos", "Restauración", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                }
                else
                    MessageBox.Show(@"No haz hecho ningun respaldo anteriormente (o no está en la ruta correcta)", "Restauracion", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }

Back up a Data Base from C#

 private void btnGuardar_Click(object sender, EventArgs e)
        {
                bool desea_respaldar = true;
                Cursor.Current = Cursors.WaitCursor;

                if (Directory.Exists(txtPath.Text))
                {
                    if (File.Exists(txtPath.Text + txtDireccion.Text+ ".bak"))
                    {
                        if (MessageBox.Show(@"Archivo existe ¿desea remplazarlo?", "Respaldo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            File.Delete(txtPath.Text + txtDireccion.Text + ".bak");
                        }
                        else
                            desea_respaldar = false;
                    }
                }
                else
                    Directory.CreateDirectory(txtPath.Text);


                if (desea_respaldar)
                {
                    if (Program.laconexion.State != System.Data.ConnectionState.Open)
                        Program.laconexion.Open();
                    SqlCommand command;
                    command = new SqlCommand(@"backup database FABRINOX to disk ='" + txtPath.Text + txtDireccion.Text + ".bak" + "' with init,stats=10", Program.laconexion);
                    command.ExecuteNonQuery();

                    Program.laconexion.Close();

                    MessageBox.Show("Respaldo Exitoso");
                    this.Close();
                }
          
        }