Solved

Sending MySQL commands using vb.net

Posted on 2010-11-11
4
767 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The solution works, but is not the ideal way to solve it.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now