Monday, April 19, 2010

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

No comments:

Post a Comment