Member Avatar for සශික

I use below code to backup mySQL database. I want to save sql database by save file dialog.
I use this,

Sub CreateBackupc()
        Dim mysqldumpPath As String = "C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin\mysqldump.exe"
         Dim host As String = "localhost"
        Dim user As String = "root"
        Dim pswd As String = "mysql"
        Dim dbnm As String = "mcs"
        Dim cmd As String = String.Format("-h{0} -u{1} -p{2} {3}", host, user, pswd, dbnm)
        SaveFileDialog1.Filter = "SQL Files (*.sql*)|*.sql"
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK _
        Then
            My.Computer.FileSystem.WriteAllText _
            (SaveFileDialog1.FileName, "mcs.sql", True)

            Dim startInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo()
            startInfo.FileName = mysqldumpPath
            startInfo.Arguments = cmd
            startInfo.RedirectStandardError = True
            startInfo.RedirectStandardInput = False
            startInfo.RedirectStandardOutput = True
            startInfo.UseShellExecute = False
            startInfo.CreateNoWindow = True
            startInfo.ErrorDialog = False
            Dim proc As System.Diagnostics.Process = New System.Diagnostics.Process()
            proc.StartInfo = startInfo
            AddHandler proc.OutputDataReceived, AddressOf OnDataReceivedc
            proc.Start()
            proc.BeginOutputReadLine()
            proc.WaitForExit()
            OutputStreamc.Flush()
            OutputStreamc.Close()
            proc.Close()
        End If
    End Sub

but it save richbox text to sql type file

Recommended Answers

All 6 Replies

To backup data you must have to execute a process to run mysqldump.exe file, which resides in C:\Program Files\MySQL\MySQL Server 5.?\bin\ folder.

The syntax is mysqldump --host=[HOSTNAME] --user=[USER] --password=[PASSWORD] -R [DATABASE NAME] > [PATH TO BACKUP FILE].

To restore data you must have to execute a process to run mysql.exe file.
The syntax is mysql --host=[HOSTNAME] --user=[USER] --password=[PASSWORD] [DATABASE NAME] < [PATH TO BACKUP FILE].

I don't want to restore backup file from this code.
I want to save backup file using save file dialog

I just write the syntax to back-up and restore .
But use is yours choice.
To back-up mysql database you have to run mysqldump.exe file

i think this code work for you...........................................

Imports System.Data.SqlClient

Public Class Form1
    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Dim dread As SqlDataReader

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        server(".")
        server(".\sqlexpress")
    End Sub

     Sub server(ByVal str As String)
        con = New SqlConnection("Data Source=" & str & ";Database=Master;integrated security=SSPI;")
        con.Open()
        cmd = New SqlCommand("select *  from sysservers  where srvproduct='SQL Server'", con)
        dread = cmd.ExecuteReader
        While dread.Read
            cmbserver.Items.Add(dread(2))
        End While
        dread.Close()
    End Sub

    Sub connection()
        con = New SqlConnection("Data Source=" & Trim(cmbserver.Text) & ";Database=Master;integrated security=SSPI;")
        con.Open()
        cmbdatabase.Items.Clear()
        cmd = New SqlCommand("select * from sysdatabases", con)
        dread = cmd.ExecuteReader
        While dread.Read
            cmbdatabase.Items.Add(dread(0))
        End While
        dread.Close()
    End Sub

    Private Sub cmbserver_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbserver.SelectedIndexChanged
        connection()
    End Sub

    Sub query(ByVal que As String)
        On Error Resume Next
        cmd = New SqlCommand(que, con)
        cmd.ExecuteNonQuery()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If ProgressBar1.Value = 100 Then
            Timer1.Enabled = False
            ProgressBar1.Visible = False
            MsgBox("Successfully Done")
        Else
            ProgressBar1.Value = ProgressBar1.Value + 5
        End If
    End Sub

    Sub blank(ByVal str As String)
        If cmbserver.Text = "" Or cmbdatabase.Text = "" Then
            MsgBox("Server Name & Database Blank Field")
            Exit Sub
        Else
            If str = "backup" Then
                SaveFileDialog1.FileName = cmbdatabase.Text
                SaveFileDialog1.ShowDialog()
                Timer1.Enabled = True
                ProgressBar1.Visible = True
                Dim s As String
                s = SaveFileDialog1.FileName
                query("backup database " & cmbdatabase.Text & " to disk='" & s & "'")
            ElseIf str = "restore" Then
                OpenFileDialog1.ShowDialog()
                Timer1.Enabled = True
                ProgressBar1.Visible = True
                query("RESTORE DATABASE " & cmbdatabase.Text & " FROM disk='" & OpenFileDialog1.FileName & "'")
            End If
        End If
    End Sub

    Private Sub cmbbackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbbackup.Click
        blank("backup")
    End Sub

    Private Sub cmdrestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdrestore.Click
        blank("restore")
    End Sub
End Class
commented: OP wants for mysql not for mssql -1

thank yOU sir

Heading Here

im having trouble with cmbserver and cmbdatabase it is showing they are not declared

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.