Link to home
Start Free TrialLog in
Avatar of pdi656
pdi656

asked on

Sending MySQL commands using vb.net

I'm trying to set up a Windows form to send certain commands to a MySQL database. The following code will successfully open up the MySQL command prompt and log in.
 
Dim p As New Process()
        Dim pInfo As New ProcessStartInfo()

        pInfo.FileName = "mysql.exe"
        pInfo.WorkingDirectory = "C:\Program Files\MySQL\MySQL Server 5.1\bin\"
        pInfo.Arguments = "--host=" & stSlaveIP & " --user=" & stUSER & " --password=" & stPWD
        p.StartInfo = pInfo
        p.Start()

Open in new window


When I use a streamwriter (depicted below) however and attempt to pass commands to it, the window I open goes black and nothing gets written or executed. "SHOW MASTER STATUS" should respond back with data and doesn't; when I issue "RESET MASTER", the function doesn't happen, so I know it's not working.
 
Dim p As New Process()
        Dim pInfo As New ProcessStartInfo()

        pInfo.FileName = "mysql.exe"
        pInfo.WorkingDirectory = "C:\Program Files\MySQL\MySQL Server 5.1\bin\"
        pInfo.Arguments = "--host=" & stSlaveIP & " --user=" & stUSER & " --password=" & stPWD
        pInfo.RedirectStandardInput = True
        pInfo.RedirectStandardOutput = True
        pInfo.UseShellExecute = False 'required to redirect
        p.StartInfo = pInfo
        p.Start()

        Dim SW As System.IO.StreamWriter = p.StandardInput
        Dim SR As System.IO.StreamReader = p.StandardOutput

        SW.WriteLine("SHOW MASTER STATUS;")
        MsgBox(SR.ReadToEnd) 'returns results of the command window
        SW.WriteLine("exit") 'exits command prompt window
        SW.Close()
        SR.Close()
        p.WaitForExit()

Open in new window


Does anyone know of a way to send commands to MySql.exe using vb.net? By the way, I have had success using mysqldump.exe and mysqladmin.exe to send commands (both in vb.net and in .bat files), but for some reason, mysql.exe seems to behave differently than those programs. Unfortunately, there are certain commands that I'd like to leverage that can only be done in mysql.exe (to my knowledge, anyways) so I really need to find a way to leverage it.

Thanks in advance.
Avatar of tomnorra
tomnorra

maybe try putting your command p.Start() after you set your Stream reader and stream writer but before writing your lines. It may just be a timing issue.
ASKER CERTIFIED SOLUTION
Avatar of pdi656
pdi656

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pdi656

ASKER

I'm not objecting to my solution - it works, however the solution is a work-around and not a direct way to handle in vb.net.
Avatar of pdi656

ASKER

The solution works, but is not the ideal way to solve it.