Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 853827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:49:27+00:00 2026-05-15T07:49:27+00:00

I need to execute the below command in command prompt. C:\MySQL\MySQL Server 5.0\bin>mysql -uroot

  • 0

I need to execute the below command in command prompt.

C:\MySQL\MySQL Server 5.0\bin>mysql -uroot -ppassword < d:/admindb/aar.sql

When i do this manually in cmd, i am getting my results.

Now i am trying to do this programatically, to execute it in cmd from c# code.

I am using the below code to do it. I am not getting any errors and Result !!!

When i debug, i get the value of string commandLine as below,

"\"C:\\MySQL\\MySQL Server 5.0\\bin\\\" -uroot -ppassword > \"D:/admindb/AAR12.sql"

I guess the problem is with this string, passed to cmd. How to solve this ??.

public void Execute()
{
    string commandLine = "\"" + MySqlCommandPath + "\"" + " -u" + DbUid + " -p" + DbPwd + " > " + "\"" + Path.Combine(Path_Backup, FileName_Backup + ExcID + ".sql");
    System.Diagnostics.ProcessStartInfo PSI = new System.Diagnostics.ProcessStartInfo("cmd.exe");
    PSI.RedirectStandardInput = true;
    PSI.RedirectStandardOutput = true;
    PSI.RedirectStandardError = true;
    PSI.UseShellExecute = false;
    System.Diagnostics.Process p = System.Diagnostics.Process.Start(PSI);
    System.IO.StreamWriter SW = p.StandardInput;
    System.IO.StreamReader SR = p.StandardOutput;
    SW.WriteLine(commandLine);
    SW.Close();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:49:28+00:00Added an answer on May 15, 2026 at 7:49 am

    I used a more object oriented approch for the same task.

    I read the file using a StreamReader and use console redirection to directly injecting the text to the mysql exe file. Works like a charm.

    Update: Here is the code, it’s VB not C# but should be easy to translate.
    I use this code to create an empty db for some unit tests. The advantage over just starting mysql from the commandline with an file as input is that

    a) I create an exception if something fails
    b) You could easily use this code to control mysql without needing a real file.
    c) In a TestProject if the Exception is thrown you get the error output from mysql directly in your TestProtocol.

    Please note that UserName, UserName, Password, Schema are Shared Properties in my class, you can just replace them with strings or modify the function call.

    Public Shared Sub SetupDatabase()
    
        ' basic configuration
        Dim infile As String = "..\..\..\MyProject\initial_db.sql"
        Dim mysql_binary As String = "mysql.exe"
        Dim mysql_args As String = String.Format("-h {0} -u{1} -p{2} {3}", HostName, UserName, Password, Schema)
    
        ' Creates a StreamReader from infile
        Dim reader As New StreamReader(infile)
    
        ' Create the process
        Dim p As New Process()
        p.StartInfo.FileName = mysql_binary
        p.StartInfo.Arguments = mysql_args
    
        ' Redirect input/output/stderr
        p.StartInfo.RedirectStandardOutput = True
        p.StartInfo.RedirectStandardError = True
        p.StartInfo.RedirectStandardInput = True
        p.StartInfo.UseShellExecute = False
        p.StartInfo.CreateNoWindow = True
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    
        ' start the process
        p.Start()
    
        ' read from infile and pass it to mysql
        Do While reader.EndOfStream = False
            p.StandardInput.WriteLine(reader.ReadLine)
        Loop
    
        ' close stdin
        p.StandardInput.Close()
    
        ' read from stdout / stderr
        Dim stdout As String = p.StandardOutput.ReadToEnd()
        Dim stderr As String = p.StandardError.ReadToEnd()
    
        ' wait for mysql to stop
        p.WaitForExit()
    
        ' if exitcode != 0 we raise an exception and include the output from stderr
        If p.ExitCode <> 0 Then
            Throw New Exception("Initial DB Setup failed with Exitcode " & p.ExitCode & ":" & vbNewLine & stderr)
        End If
    
        p.Close()
        reader.Close()
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need execute an SQL query in ORACLE it takes a certain amount of
I need to execute a SQL Query between two databases. Example: SELECT * from
I need to execute a command 100-200 times, and so far my research indicates
i want to backup firebird database. Below is command that i use to execute
I'm new to Linux. In the command below, why do I need to use
I have a scenario in which I need to execute the below statement. air
I need execute some code before Windows shutdown process each time. So, I want
I need to execute a subquery in a select clause with Apache Openjpa 2.
I need to execute some commands in batch mode (e.g., via Rscript). They work
I need to execute code at intervals, sometimes 10 seconds, sometimes 5 minutes. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.