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

No comments:

Post a Comment