0% found this document useful (0 votes)
79 views3 pages

Imports System - Data.OleDb

This document contains code for creating database backups in Visual Basic. It includes: 1. A method to browse for and select a backup directory when a button is clicked. 2. A method that is called when the Create Backup button is clicked. It validates the required fields are populated, then calls the crear_backup() method to perform the backup. 3. The crear_backup() method that constructs the SQL backup command and executes it to backup the selected database to the specified file path. It returns a boolean on the backup success.

Uploaded by

Daniel Diaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views3 pages

Imports System - Data.OleDb

This document contains code for creating database backups in Visual Basic. It includes: 1. A method to browse for and select a backup directory when a button is clicked. 2. A method that is called when the Create Backup button is clicked. It validates the required fields are populated, then calls the crear_backup() method to perform the backup. 3. The crear_backup() method that constructs the SQL backup command and executes it to backup the selected database to the specified file path. It returns a boolean on the backup success.

Uploaded by

Daniel Diaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Imports System.Data.

OleDb
Public Class FBackup

Private Sub btnexaminar_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnexaminar.Click
Dim Directorio As New FolderBrowserDialog

If Directorio.ShowDialog = Windows.Forms.DialogResult.OK Then


Me.txtdirectorio.Text = Directorio.SelectedPath
txtnombackup.Enabled = True
txtnombackup.Focus()
End If
End Sub

Private Sub btncrear_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btncrear.Click
GroupBox1.Enabled = False
btncrear.Enabled = False
' If cboservidor.Text <> "" Then
If Me.cbobasedatos.Text <> "" Then
If txtdirectorio.Text <> "" Then
If txtnombackup.Text <> "" Then
' If txtobs.Text <> "" Then

If crear_backup() = True Then


MessageBox.Show("Backup creado satisfactoriamnete", "OK",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Error al crear el Backup", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
'Else
' MessageBox.Show("Ingrese una descripcion del Backup",
"Aviso", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
'End If
Else
MessageBox.Show("Ingrese el nombre del Backup", "Aviso",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Else
MessageBox.Show("Seleccione la ruta donde se creara el Backup",
"Aviso", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Else
MessageBox.Show("Seleccione la Base de Datos", "Aviso",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

End If
' Else
' MessageBox.Show("Ingrese el Nombre del Servidor de Datos SQL", "Aviso",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
' End If
End Sub
Private Function crear_backup() As Boolean
If txtdirectorio.Text.Length <> 3 Then
txtdirectorio.Text = txtdirectorio.Text + "\" + txtnombackup.Text +
".bak"
Else
txtdirectorio.Text = txtdirectorio.Text + txtnombackup.Text + ".bak"
End If
Try

Dim sCmd As String = "BACKUP DATABASE [" + cbobasedatos.Text + "] TO


DISK = N'" + txtdirectorio.Text + "' " &
"WITH NOFORMAT, NOINIT, " &
"NAME = N'" + txtnombackup.Text + "', SKIP, NOREWIND, NOUNLOAD, STATS =
10"
Dim cmd As New OleDbCommand(sCmd, Conn)
cmd.ExecuteNonQuery()
crear_backup = True
Catch ex As Exception
crear_backup = False
MessageBox.Show(ex.Message)
Finally
Conn.Close()
End Try
' End Using
'"WITH DESCRIPTION = N'" + txtobs.Text + "', NOFORMAT, NOINIT, " & _''' si
deseas con desc antes de with
End Function
Private Sub cbobasedatos_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cbobasedatos.SelectedIndexChanged
btnexaminar.Enabled = True
End Sub

Private Sub txtnombackup_KeyPress(ByVal sender As Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs) Handles txtnombackup.KeyPress
If e.KeyChar = Convert.ToChar(Keys.Return) Then
btncrear.Enabled = True
btncrear.Focus()
End If
End Sub

Private Sub txtnombackup_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles txtnombackup.TextChanged

End Sub

Private Sub txtobs_KeyPress(ByVal sender As Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs)
If e.KeyChar = Convert.ToChar(Keys.Return) Then
btncrear.Enabled = True
btncrear.Focus()
End If

End Sub
Private Sub FBackup_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AbrirConexion()

txtdirectorio.Enabled = False
btnexaminar.Enabled = False
btncrear.Enabled = False
txtnombackup.Enabled = False

Dim Consulta As String ' = '"SELECT * FROM Oldbojects WHERE type=1 AND
flags=0"
Dim ds As DataSet = New DataSet()
Dim Cmd As New OleDbDataAdapter(Consulta, Conn)
Cmd.Fill(ds, "bd")
Dim dt As New DataTable
dt = ds.Tables("bd")
cbobasedatos.DataSource = dt
cbobasedatos.DisplayMember = dt.Columns(0).ToString
cbobasedatos.ValueMember = dt.Columns(0).ToString
End Sub
End Class

You might also like