using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Diagnostics; using System.Management; using System.Threading; namespace GummingConfig { public partial class FormMain : Form { public FormMain() { InitializeComponent(); this.Load += FormMain_Load; } private void FormMain_Load(object sender, EventArgs e) { } private void btnExecute_Click(object sender, EventArgs e) { try { this.Enabled = false; string connection = string.Format("Server={0};User={1};Password={2};Use Procedure Bodies=false;Charset=utf8;Allow Zero Datetime=True; Pooling=false; Max Pool Size=50;", txtInstance.Text, txtUsername.Text, txtPassword.Text); MysqlHelper.connectionString = connection; try { MysqlHelper.ExecuteNonQuery("Drop DATABASE IF EXISTS habitusdb;"); } catch { } try { string basePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"MySQLServer"); string datafolder = Path.Combine(basePath, "data\\habitusdb"); System.IO.Directory.Delete(datafolder, true); } catch { } MysqlHelper.ExecuteNonQuery("CREATE DATABASE IF NOT EXISTS habitusdb default character set utf8 COLLATE utf8_general_ci;"); connection = string.Format("Server={0};Database=habitusdb; User={1};Password={2};Use Procedure Bodies=false;Charset=utf8;Allow Zero Datetime=True; Pooling=false; Max Pool Size=50;", txtInstance.Text, txtUsername.Text, txtPassword.Text); MysqlHelper.connectionString = connection; string sqlfile = Path.Combine(Environment.CurrentDirectory, "Database\\backup.sql"); if (!File.Exists(sqlfile)) throw new Exception("数据库文件不存在"); string sql = System.IO.File.ReadAllText(sqlfile); sql = "use habitusdb;" + sql; MysqlHelper.ExecuteNonQuery(sql); MessageBox.Show("成功设置默认数据库,系统配置完成后请在桌面双击运行中医体质自动识别系统"); this.Close(); } catch (Exception ep) { this.Enabled = true; MessageBox.Show("发生异常: " + ep.Message); } } private void btnInstall_Click(object sender, EventArgs e) { this.Enabled = false; this.Cursor = Cursors.WaitCursor; System.Diagnostics.Process.Start("regsvr32", "\"" + System.Windows.Forms.Application.StartupPath + "\\IRImage\\IRImage.OCX\" /s"); string str = string.Empty; string query = "SELECT Name FROM Win32_Product WHERE Name LIKE '%MySQL Server%'"; var searcher = new ManagementObjectSearcher("root\\CIMV2", query); foreach (ManagementObject service in searcher.Get()) { str += service["Name"]; break; } if(string.IsNullOrEmpty(str)) { string basePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"MySQLServer"); string inifile = Path.Combine(basePath, "my.ini"); StreamWriter sw = new StreamWriter(inifile, false); sw.WriteLine("[client]"); sw.WriteLine("port=3306"); sw.WriteLine("default-character-set=utf8"); sw.WriteLine("[mysqld]"); sw.WriteLine("port=3306"); sw.WriteLine("character_set_server=utf8"); sw.WriteLine("basedir=" + basePath); sw.WriteLine("datadir=" + Path.Combine(basePath, "data")); sw.WriteLine("sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES "); sw.Flush(); sw.Close(); ExecCommand(Path.Combine(basePath, @"bin\mysqld.exe"), " install MySQL --defaults-file=\"" + inifile + "\""); Process process = new Process(); process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = false; process.StartInfo.FileName = "cmd.exe"; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.Start(); process.StandardInput.WriteLine("cd\\"); process.StandardInput.WriteLine("net start mysql"); process.StandardInput.WriteLine("exit"); process.WaitForExit(); string output = process.StandardOutput.ReadToEnd(); process.Close(); Thread.Sleep(3000); MessageBox.Show("成功安装Mysql数据库,请初始化数据库"); } this.Enabled = true; this.Cursor = Cursors.Arrow; btnInstall.Enabled = false; btnExecute.Enabled = true; } private void ExecCommand(string command, string parameter) { System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(command, parameter); procStartInfo.RedirectStandardOutput = true; procStartInfo.UseShellExecute = false; procStartInfo.CreateNoWindow = true; System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo = procStartInfo; proc.Start(); Thread.Sleep(10000); proc.WaitForExit(); } } }