?
Solved

Sending MySQL commands using vb.net

Posted on 2010-11-11
4
Medium Priority
?
816 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

650 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