Solved

Sending MySQL commands using vb.net

Posted on 2010-11-11
4
783 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:pdi656
  • 3
4 Comments
 
LVL 3

Expert Comment

by:tomnorra
ID: 34136386
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.
0
 
LVL 1

Accepted Solution

by:
pdi656 earned 0 total points
ID: 34282855
The position of p.Start() didn't make a difference. Through trial and error, I found that I could execute these commands with a .bat file with ease. As a result, I had the vb code write and execute a .bat file. This solution is not ideal, but it works.
0
 
LVL 1

Author Comment

by:pdi656
ID: 34282878
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.
0
 
LVL 1

Author Closing Comment

by:pdi656
ID: 34324320
The solution works, but is not the ideal way to solve it.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 51
Help needed with Powershell  XML to MySQL 5 46
ASP/VB email question 4 35
How do uses indexes to maximize MySQL Searches 14 35
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question