/// <summary> /// Verify sql parameters user specified. /// </summary> /// <returns></returns> private sqlCheckResult VerifySqlParam() { sqlCheckResult result = sqlCheckResult.Unknown; // let's see if SqlConnection is correct. SqlConnection connection = new SqlConnection(); connection.ConnectionString = string.Format("Server={0};User Id={1};Password={2};Connection Timeout=20;", _serverName.Text, _adminName.Text, _adminPassword.Text); try { connection.Open(); string dbName = ConfigurationManager.AppSettings["databaseName"]; if (IsDbExists(connection, dbName)) { // DB exists. DialogResult dr = MessageBox.Show("The target dababase exists already\nClick Yes to bypass the database setup, No to cancel the whole process", "Server Setup", MessageBoxButtons.YesNo); if (dr == DialogResult.Yes) { result = sqlCheckResult.BypassSqlSetup; } else { result = sqlCheckResult.UserCancel; } } else { result = sqlCheckResult.Okay; } connection.Close(); } catch (SqlException se) { System.Diagnostics.Debug.WriteLine(se.Message); MessageBox.Show(se.Message); result = sqlCheckResult.Invalid; } return(result); }
/// <summary> /// Event handler when user pressed setup button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _configureButton_Click(object sender, EventArgs e) { sqlCheckResult result = VerifySqlParam(); if (result == sqlCheckResult.Invalid || result == sqlCheckResult.UserCancel || result == sqlCheckResult.Unknown) { return; } // make sure the target db does not exist. // and bypass if exists if (result != sqlCheckResult.BypassSqlSetup) { bool runOK = RunScript(); if (!runOK) { return; } } string dbName = ConfigurationManager.AppSettings["databaseName"]; string targetConfig = ConfigurationManager.AppSettings["configFileName"]; if (!string.IsNullOrEmpty(targetConfig)) { XElement root = XElement.Load(targetConfig); if (root != null) { bool modified = false; XElement settingElement = root.Element("appSettings"); if (settingElement != null) { foreach (XElement child in settingElement.Elements()) { if (child.Name == "add") { XAttribute keyAttr = child.FirstAttribute; if (keyAttr != null && keyAttr.Name == "key" && keyAttr.Value == "baseAddress") { XAttribute valAttr = keyAttr.NextAttribute; if (valAttr != null) { valAttr.Value = string.Format("http://{0}/EjsWcfSvc/EjsPublic.svc", _serviceName.Text); modified = true; } } else if (keyAttr != null && keyAttr.Name == "key" && keyAttr.Value == "connectionString") { XAttribute valAttr = keyAttr.NextAttribute; if (valAttr != null) { valAttr.Value = string.Format("Server={0};Database={1};User Id={2};Password={3};Connection Timeout=90;", _serverName.Text, dbName, _adminName.Text, _adminPassword.Text); modified = true; } } } } } if (modified) { root.Save("temp.xml"); root = null; File.Copy("temp.xml", targetConfig, true); // and delete the temp File.Delete("temp.xml"); } RunWCFReg(); // @todo: run MSI setup here. RunMsiSetup(); // Very weired, but anyhow, we may need to run WCFReg twice... RunWCFReg(); // OK finished. //MessageBox.Show("Server setup finished"); // delay the exit somewhat this.Close(); } } }