C# 整合mysql.exe+mysqldump.exe 實現將 遠端資料庫的資料表資料 複製到 本地端的資料庫之中
C# 整合mysql.exe+mysqldump.exe 實現將 遠端資料庫的資料表資料 複製到 本地端的資料庫之中
public class External_MySQL
{
public string dbHost;// = “127.0.0.1”;//資料庫位址
public string dbport;// = “3306”;
public string dbUser;// = “root”;//資料庫使用者帳號
public string dbPass;// = “usbw”;//資料庫使用者密碼
public string dbName;// = “v8_workstation”;//資料庫名稱
public string connStr;// = “server=” + dbHost + “;Port=” + dbport + “;uid=” + dbUser + “;pwd=” + dbPass + “;database=” + dbName + “;charset=utf8”;
public MySqlConnection m_conn = null;
public String m_StrLastSQL;// = “”;
public Process m_pro;
public External_MySQL()
{
dbHost=””;
dbport=””;
dbUser=””;
dbPass=””;
dbName=””;
}
public bool CheckMySQL(String StrdbHost, String Strdbport, String StrdbUser, String StrdbPass)
{
bool blnAns = true;
Thread.Sleep(1000);
dbHost = StrdbHost;
dbport = Strdbport;
dbUser = StrdbUser;
dbPass = StrdbPass;
connStr = “server=” + dbHost + “;Port=” + dbport + “;uid=” + dbUser + “;pwd=” + dbPass + “;”;
m_conn = new MySqlConnection(connStr);
try
{
m_conn.Open();
if (m_conn.State == ConnectionState.Open)
{
blnAns = true;
m_conn.Close();
}
else
{
blnAns = false;
}
}
catch
{
blnAns = false;
}
m_conn = null;
return blnAns;
}
public bool DownloadDBTable(String StrTableName, String StrDBName = “SYRISCloudSystem”)
{
bool blnAns = true;
String StrCmd = “”;
dbName = StrDBName;
string path = System.Windows.Forms.Application.StartupPath;
//-h192.168.1.196 -P3306 -uhino -phinohino -e -q –default-character-set=utf8 SYRISCloudSystem system_config > system_config.sql
StrCmd = String.Format(“\”” + path + “\\mysql\\bin\\mysqldump.exe\” -h{0} -P{1} -u{2} -p{3} -t -e -q –complete-insert –extended-insert=FALSE –default-character-set=utf8 {4} {5} > {5}.sql”, dbHost, dbport, dbUser, dbPass, dbName, StrTableName);
path += “\\mysql\\bin\\mysqldump.bat”;
StreamWriter sw = new StreamWriter(path);
sw.WriteLine(StrCmd);// 寫入文字
//sw.WriteLine(“pause”);// 寫入文字
sw.Close();// 關閉串流
if (m_pro == null)
{
ProcessStartInfo startInfo = new ProcessStartInfo(path);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
try
{
m_pro = Process.Start(startInfo);
}
catch
{
return false;//找不到執行檔的防呆 at 2017/06/16
}
Thread.Sleep(100);
if (m_pro != null)
{
m_pro.WaitForExit();//下載SERVER資料
m_pro = null;
StreamReader sr = new StreamReader(StrTableName + “.sql”, Encoding.ASCII);
StreamWriter sw1 = new StreamWriter(“INSERTDB.sql”,false,Encoding.ASCII);
sw1.WriteLine(“USE `v8_workstation`;”);// 寫入文字
while (!sr.EndOfStream)// 每次讀取一行,直到檔尾
{
string line = sr.ReadLine();// 讀取文字到 line 變數
if (line.Contains(“INSERT INTO”))
{
sw1.WriteLine(line);// 寫入文字
}
}
sw1.Close();// 關閉串流
sr.Close();// 關閉串流
System.IO.File.Delete(StrTableName + “.sql”);
System.IO.File.Delete(path);
MySQL.ClearTable(StrTableName);
path = System.Windows.Forms.Application.StartupPath;
StrCmd = “\”” + path + “\\mysql\\bin\\mysql.exe\” -uroot -pusbw -P 3307 < INSERTDB.sql”;
path += “\\mysql\\bin\\mySQL_Import.bat”;
StreamWriter sw2 = new StreamWriter(path);
sw2.WriteLine(StrCmd);// 寫入文字
//sw2.WriteLine(“pause”);// 寫入文字
sw2.Close();// 關閉串流
ProcessStartInfo startInfo1 = new ProcessStartInfo(path);
startInfo1.WindowStyle = ProcessWindowStyle.Hidden;
m_pro = Process.Start(startInfo1);
try
{
m_pro = Process.Start(startInfo1);
}
catch
{
return false;//找不到執行檔的防呆 at 2017/06/16
}
m_pro.WaitForExit();//下載SERVER資料
System.IO.File.Delete(path);
blnAns = true;
}
else
{
blnAns = false;
}
}
m_pro = null;
return blnAns;
}
}