Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi i want to run the mysql script from the batch file. In order to do that when i was try to login it was failing. How to login to mysql and run script?

What I have tried:

Below is the script i have tried.
C#
if (!File.Exists(batFilePath))
            {
                using (FileStream fs = File.Create(batFilePath))
                {
                    fs.Close();
                }
            }
            using (StreamWriter sw = new StreamWriter(batFilePath))
            {
                sw.WriteLine("mysql -u techu -p");  //techu is username 
                sw.WriteLine("12341234");   // password
                sw.WriteLine("source H:\testscript.sql");
                sw.WriteLine("@echo done");
                //sw.WriteLine("pause");
            }
Posted
Updated 21-Feb-20 1:00am

1 solution

You cannot define a command line on several lines. It has to be continuous.
C#
using (StreamWriter sw = new StreamWriter(batFilePath))
{
   sw.Write("mysql -u techu -p");  //techu is username 
   sw.Write("12341234");   // password
   sw.WriteLine(" < H:\testscript.sql");
   sw.WriteLine("@echo done");
   //sw.WriteLine("pause");
}
MySQL :: MySQL 5.7 Reference Manual :: 4.5.1 mysql — The MySQL Command-Line Client[^]
 
Share this answer
 
Comments
jung lee 21-Feb-20 7:19am    
if we write "mysql -u techu -p" in command prompt, in next step it prompts to enter password. if password is correct it enters to mysql mode. then if we enter "source<space> path/of/sqlscript then it executes that sql script file.
Here what is happening is, it is not taking password because sw.writeline is giving password as a new command. at that step it is failing to execute.
phil.o 21-Feb-20 7:56am    
password option[^]
If you provide the password on the command line, it will not be prompted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900