Solved

Sending MySQL commands using vb.net

Posted on 2010-11-11
4
800 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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 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