private void btSetDbUserPw_Click(object sender, EventArgs e) { SetPwForm spf = new SetPwForm(); spf.ShowDialog(this); if (spf.pwSet) { //string conn = "Server=" + spf.srvIP + ";Port=" + spf.srvPort + ";User Id=postgres;Password="******";Database=endoDB;"; string conn = "Host=" + spf.srvIP + ";Port=" + spf.srvPort + ";Username=postgres;Password="******";" + sslString; functions.functionResult rst; SetDbUserPw sdup = new SetDbUserPw(); sdup.ShowDialog(this); if (sdup.setPw) { rst = functions.doSQL(conn, "ALTER ROLE db_user WITH ENCRYPTED PASSWORD \'" + sdup.pw + "\';"); if (rst == functions.functionResult.failed) { MessageBox.Show(Properties.Resources.DataBaseError, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else if (rst == functions.functionResult.connectionError) { MessageBox.Show(Properties.Resources.ConnectFailed, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } MessageBox.Show(Properties.Resources.UpdateDone, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { sdup.Dispose(); return; } } spf.Dispose(); }
private void bt_do_sql_Click(object sender, EventArgs e) { SetPwForm spf = new SetPwForm(); spf.ShowDialog(this); if (spf.pwSet) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "SQL(*.sql;*.txt)|*.sql;*.txt|All Files(*.*)|*.*"; ofd.FilterIndex = 1; ofd.Title = Properties.Resources.SelectSQLFile; string sql_path; if (ofd.ShowDialog() == DialogResult.OK) { sql_path = ofd.FileName; } else { return; } string conn = "Server=" + spf.srvIP + ";Port=" + spf.srvPort + ";User Id=postgres;Password="******";Database=endoDB;" + sslString; functions.functionResult rst; if (File.Exists(sql_path)) { StreamReader file = new StreamReader(sql_path); List<string> SQLs = new List<string>(); string line; while (!string.IsNullOrWhiteSpace((line = file.ReadLine()))) { SQLs.Add(line); } for (int i = 0; i < SQLs.Count; i++) { //MessageBox.Show(SQLs[i]); rst = functions.doSQL(conn, SQLs[i]); if (rst == functions.functionResult.failed) { MessageBox.Show("[SQL: Line " + (i + 1).ToString() + "]" + Properties.Resources.DataBaseError, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (rst == functions.functionResult.connectionError) { MessageBox.Show("[SQL: Line " + (i + 1).ToString() + "]" + Properties.Resources.ConnectFailed, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } MessageBox.Show("[SQL File]" + Properties.Resources.ProcedureFinished, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("[SQL File]" + Properties.Resources.FileNotExist, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } spf.Dispose(); }
private void btSetDbUserPw_Click(object sender, EventArgs e) { SetPwForm spf = new SetPwForm(); spf.ShowDialog(this); if (spf.pwSet) { //string conn = "Server=" + spf.srvIP + ";Port=" + spf.srvPort + ";User Id=postgres;Password="******";Database=endoDB;"; string conn = "Server=" + spf.srvIP + ";Port=" + spf.srvPort + ";User Id=postgres;Password="******";" + sslString; functions.functionResult rst; SetDbUserPw sdup = new SetDbUserPw(); sdup.ShowDialog(this); if (sdup.setPw) { rst = functions.doSQL(conn, "ALTER ROLE db_user WITH ENCRYPTED PASSWORD \'" + sdup.pw + "\';"); if (rst == functions.functionResult.failed) { MessageBox.Show(Properties.Resources.DataBaseError, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else if (rst == functions.functionResult.connectionError) { MessageBox.Show(Properties.Resources.ConnectFailed, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } MessageBox.Show(Properties.Resources.UpdateDone, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { sdup.Dispose(); return; } } spf.Dispose(); }
private void bt_do_sql_Click(object sender, EventArgs e) { SetPwForm spf = new SetPwForm(); spf.ShowDialog(this); if (spf.pwSet) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "SQL(*.sql;*.txt)|*.sql;*.txt|All Files(*.*)|*.*"; ofd.FilterIndex = 1; ofd.Title = Properties.Resources.SelectSQLFile; string sql_path; if (ofd.ShowDialog() == DialogResult.OK) { sql_path = ofd.FileName; ofd.Dispose(); } else { ofd.Dispose(); spf.Dispose(); return; } string connStr = "Host=" + spf.srvIP + ";Port=" + spf.srvPort + ";Username=postgres;Password="******";Database=endoDB;" + sslString; if (File.Exists(sql_path)) { string SQL = ""; using (StreamReader file = new StreamReader(sql_path)) { try { while (file.Peek() >= 0) { string buffer = file.ReadLine(); SQL += buffer + Environment.NewLine; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); throw; } finally { if (file != null) { file.Close(); } } } try { using (var conn = new NpgsqlConnection(connStr)) { try { conn.Open(); } catch (NpgsqlException npe) { MessageBox.Show(Properties.Resources.CouldntOpenConn + "\r\n" + npe.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); conn.Close(); } catch (IOException ioe) { MessageBox.Show(Properties.Resources.ConnClosed + "\r\n" + ioe.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); conn.Close(); } if (conn.State == ConnectionState.Open) { using (var cmd = new NpgsqlCommand()) { try { cmd.Connection = conn; cmd.CommandText = SQL; cmd.ExecuteNonQuery(); } catch (NpgsqlException nex) { MessageBox.Show("[NpgsqlException]\r\n" + nex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { conn.Close(); } } } else { MessageBox.Show(Properties.Resources.CouldntOpenConn, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); conn.Close(); } } } catch (ArgumentException ae) { MessageBox.Show(Properties.Resources.WrongConnectingString + "\r\n" + ae.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } spf.Dispose(); }