Erm's I.T. Girl – Zelna Ellis

Don't fear when Zel is near…

VB.NET Run MySQL Script

To run a MySQL Script in VB.NET has one trick: The command window must stay open while the script is running.

Here is an example of the arguments string to pass to the cmd.exe:
"/K ""mysql.exe"" " & "-h hostname -u username -ppassword < filename.sql"

Explanation of some of the arguments string:
/K = Carries out the command specified by the string but doesn’t terminate – cmd window remains open.
Then the MySQL values:
hostname = MySQL Server hostname
username = MySQL username
password = MySQL user’s password
filename = MYSQL script – must be full path.

Note:
I’ve encountered errors when one doesn’t specify the time in process.WaitForExit().

Code Snippet:

Private Sub RunMySQLScript(ByVal TheArguments As String)
     Try
          Dim proc As New Process
          proc.StartInfo.FileName = "cmd.exe"
          proc.StartInfo.UseShellExecute = False
          'Don’t show command prompt window.
          proc.StartInfo.CreateNoWindow = True
          proc.StartInfo.Arguments = TheArguments
          proc.Start()
          'Time in milliseconds, can change value to wait.
          proc.WaitForExit(1000)
          proc.Close()
     Catch ex As Exception
          MessageBox.Show(Err.Description, "Run MySQL Scripts", _
          MessageBoxButtons.OK, MessageBoxIcon.Error)
     End Try
End Sub

10 October 2009 - Posted by | .NET & MySQL | , , , ,

1 Comment »

  1. […] – Catch error messages from Process In previous posts, how to run MySQL scripts and add local users and groups with net command in VB.NET, it did not catch any errors from the […]

    Pingback by VB.NET – Catch error messages from Process « Erm’s I.T. Girl – Zelna Ellis | 17 October 2009 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: