private void btnImportConfig_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = @"C:\"; openFileDialog1.Title = "Browse Text Files"; openFileDialog1.CheckFileExists = true; openFileDialog1.CheckPathExists = true; openFileDialog1.DefaultExt = "txt"; openFileDialog1.Filter = "Db Config File (*.json)|*.json"; openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; openFileDialog1.ReadOnlyChecked = true; openFileDialog1.ShowReadOnly = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { string file = openFileDialog1.FileName; string tmp = File.ReadAllText(file); conModel = JsonConvert.DeserializeObject <ConnectionModelN11>(tmp); } }
public bool IsConnect(ConnectionModelN11 modelN11) { if (String.IsNullOrEmpty(databaseName)) { return(false); } string connstring = string.Format("Server={0}; Port={1}; database={2}; UID={3}; password={4}", modelN11.server, modelN11.port, modelN11.databaseName, modelN11.username, modelN11.password); connection = new MySqlConnection(connstring); try { connection.Open(); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show("Error no: 1 -" + ex.Message); return(false); } return(true); }
private void btnTestConnection_Click(object sender, EventArgs e) { if (conModel == null && txtDbName.Text.Trim() == "") { MessageBox.Show("Error no: 2 - Please enter connection info OR Import database config!"); } else { if (conModel == null) { conModel = new ConnectionModelN11(); conModel.port = txtPort.Text.Trim(); conModel.server = txtServer.Text.Trim(); conModel.username = txtUsername.Text.Trim(); conModel.password = txtPassword.Text.Trim(); conModel.databaseName = txtDbName.Text.Trim(); conModel.tableCategories = txtTableCategory.Text.Trim(); conModel.tableAttributes = txtTableAttribute.Text.Trim(); } var con = DBConnection.Instance(); con.DatabaseName = conModel.databaseName; if (con.IsConnect(conModel)) { txtPort.Text = conModel.port; txtServer.Text = conModel.server; txtUsername.Text = conModel.username; txtDbName.Text = conModel.databaseName; txtTableAttribute.Text = conModel.tableAttributes; txtTableCategory.Text = conModel.tableCategories; MessageBox.Show("Test is successful."); } con.Close(); } }