C# Call Cmd ~ 查 MYSQL 指定資料表內的筆數 ( 抓Cmd回應[重導標準輸出入] + 不要有CMD畫面 )
C# Call Cmd ~ 查 MYSQL 指定資料表內的筆數 ( 抓Cmd回應[重導標準輸出入] + 不要有CMD畫面 )
資料來源: https://stackoverflow.com/questions/23246613/hiding-the-process-window-why-isnt-it-working
public static String m_StrTableName=””;
public static bool m_blnRunCount = false;
public static void Thread_DBTableCount(object arg=null)
{
ProgressDialog d = (ProgressDialog)arg;
string[] stringSeparators = new string[] { “\r\n” };
int intCount01=0, intCount02=0;
String path = System.Windows.Forms.Application.StartupPath + “\\mysql\\bin\\”;
String StrCmd = “”;
String StrRemoteCmdFile = “remote_mySQL_cmd.bat”;
String StrLocalCmdFile = “local_mySQL_cmd.bat”;
String StrRemoteSQLFile = “remote_select_count.sql”;
String StrLocalSQLFile = “local_select_count.sql”;
String StrRemoteDBName = “SYRISCloudSystem”;
String StrLocalDBName = “v8_workstation”;
String StrRemoteSQL = “”;// String.Format(“USE `{0}`;SELECT COUNT(*) FROM {1};”, StrRemoteDBName, m_StrTableName);
String StrLocalSQL = “”;// String.Format(“USE `{0}`;SELECT COUNT(*) FROM {1};”, StrLocalDBName, m_StrTableName);
while (m_blnRunCount)
{
if (m_StrTableName.Length > 0)
{
StrRemoteSQL = String.Format(“USE `{0}`;SELECT COUNT(*) FROM {1};”, StrRemoteDBName, m_StrTableName);
StrLocalSQL = String.Format(“USE `{0}`;SELECT COUNT(*) FROM {1};”, StrLocalDBName, m_StrTableName);
StreamWriter Remote_sw01 = new StreamWriter(path + StrRemoteSQLFile);
Remote_sw01.WriteLine(StrRemoteSQL);// 寫入文字
Remote_sw01.Close();// 關閉串流
StreamWriter Local_sw01 = new StreamWriter(path + StrLocalSQLFile);
Local_sw01.WriteLine(StrLocalSQL);// 寫入文字
Local_sw01.Close();// 關閉串流
StreamWriter Remote_sw02 = new StreamWriter(path + StrRemoteCmdFile);
StrCmd = “\”” + path + “mysql.exe”+”\””;
StrCmd = StrCmd + String.Format(” -h{0} -P{1} -u{2} -p{3} < {4}”, Main_Frm.pForm1.m_ExMySQL.dbHost, Main_Frm.pForm1.m_ExMySQL.dbport, Main_Frm.pForm1.m_ExMySQL.dbUser, Main_Frm.pForm1.m_ExMySQL.dbPass, “\”” + path + StrRemoteSQLFile + “\””);
Remote_sw02.WriteLine(StrCmd);// 寫入文字
Remote_sw02.Close();// 關閉串流
StreamWriter Local_sw02 = new StreamWriter(path + StrLocalCmdFile);
StrCmd = “\”” + path + “mysql.exe” + “\””;
StrCmd = StrCmd + String.Format(” -h{0} -P{1} -u{2} -p{3} < {4}”, “127.0.0.1”, “3307”, “root”, “usbw”, “\””+path + StrLocalSQLFile + “\””);
Local_sw02.WriteLine(StrCmd);// 寫入文字 mysql.exe -uroot -pusbw -P 3307 < select_count.sql
Local_sw02.Close();// 關閉串流
ProcessStartInfo start01 = new ProcessStartInfo();
start01.FileName = path + StrRemoteCmdFile; // Specify exe name.
start01.CreateNoWindow = true;//start01.WindowStyle = ProcessWindowStyle.Minimized;
start01.UseShellExecute = false;
start01.RedirectStandardOutput = true;
using (Process p01 = Process.Start(start01))
{
// Read in all the text from the process with the StreamReader
string result01 = “”;
using (StreamReader reader01 = p01.StandardOutput)
{
result01 = reader01.ReadToEnd();
}
try
{
string[] lines01 = result01.Split(stringSeparators, StringSplitOptions.None);
for (int i = 0; i < lines01.Length; i++)
{
if (lines01[i] == “COUNT(*)”)
{
intCount01 = Convert.ToInt32(lines01[i+1]);
break;
}
}
}
catch
{
intCount01 = 0;
}
}
ProcessStartInfo start02 = new ProcessStartInfo();
start02.FileName = path + StrLocalCmdFile; // Specify exe name.
start02.CreateNoWindow = true;//start02.WindowStyle = ProcessWindowStyle.Minimized;
start02.UseShellExecute = false;
start02.RedirectStandardOutput = true;
using (Process p02 = Process.Start(start02))
{
// Read in all the text from the process with the StreamReader
string result02 = “”;
using (StreamReader reader02 = p02.StandardOutput)
{
result02 = reader02.ReadToEnd();
}
try
{
string[] lines02 = result02.Split(stringSeparators, StringSplitOptions.None);
for (int j = 0; j < lines02.Length; j++)
{
if (lines02[j] == “COUNT(*)”)
{
intCount02 = Convert.ToInt32(lines02[j+1]);
break;
}
}
}
catch
{
intCount02 = 0;
}
}
if (intCount01 < intCount02)
{
m_StrPercentage = String.Format(“{0}/{1}\n{2}”, intCount01, intCount02, m_StrTableName);
}
else
{
m_StrPercentage = String.Format(“{0}/{1}\n{2}”, intCount02, intCount01, m_StrTableName);
}
d.m_strMessage = m_StrMain + “\n” + m_StrPercentage;
}
Thread.Sleep(5000);
}
}