Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 Npgsql;
- namespace psqlTest
- {
- public partial class Form2 : Form
- {
- string host = global.host;
- string database = global.database;
- string user = global.user;
- string password = global.password;
- private string connstring = string.Format("Server={0};Port={1};" +
- "User Id = {2};Password={3};Database={4};",
- global.host, global.port, global.user,
- global.password, global.database);
- private NpgsqlConnection conn;
- private string sql;
- private NpgsqlCommand cmd;
- private DataTable dt;
- private int rowIndex = -1;
- public Form2()
- {
- InitializeComponent();
- }
- private void Form2_Load(object sender, EventArgs e)
- {
- conn = new NpgsqlConnection(connstring);
- Select();
- }
- private void Select()
- {
- try
- {
- conn.Open();
- sql = @"select * from st_select();";
- cmd = new NpgsqlCommand(sql, conn);
- dt = new DataTable();
- dt.Load(cmd.ExecuteReader());
- conn.Close();
- dvgData.DataSource = null;
- dvgData.DataSource = dt;
- }
- catch (Exception ex)
- {
- conn.Close();
- MessageBox.Show("Error: "+ex.Message);
- throw;
- }
- }
- private void dvgData_CellClick(object sender, DataGridViewCellEventArgs e)
- {
- if (e.RowIndex >= 0)
- {
- rowIndex = e.RowIndex;
- txtFirst.Text = dvgData.Rows[e.RowIndex].Cells["firstname"].Value.ToString();
- txtMiddle.Text = dvgData.Rows[e.RowIndex].Cells["midname"].Value.ToString();
- txtLast.Text = dvgData.Rows[e.RowIndex].Cells["lastname"].Value.ToString();
- }
- }
- private void btnIncert_Click(object sender, EventArgs e)
- {
- txtFirst.Enabled = txtMiddle.Enabled = txtLast.Enabled = true;
- rowIndex = -1;
- txtFirst.Text = txtMiddle.Text = txtLast.Text = null;
- txtFirst.Select();
- }
- private void btnDelete_Click(object sender, EventArgs e)
- {
- if (rowIndex < 0)
- {
- MessageBox.Show("Please choose student to delete");
- return;
- }
- try
- {
- conn.Open();
- sql = @"select * from st_delete(:_id)";
- cmd = new NpgsqlCommand(sql, conn);
- cmd.Parameters.AddWithValue("_id", int.Parse(dvgData.Rows[rowIndex].Cells["id"].Value.ToString()));
- if ((int)cmd.ExecuteScalar() == 1)
- {
- MessageBox.Show("Delete student successfully");
- rowIndex = -1;
- Select();
- }
- conn.Close();
- }
- catch (Exception ex)
- {
- conn.Close();
- MessageBox.Show("Deleted fail. Error:"+ex.Message);
- }
- }
- private void btnUpdate_Click(object sender, EventArgs e)
- {
- if(rowIndex < 0)
- {
- MessageBox.Show("Please choose student to update");
- return;
- }
- txtFirst.Enabled = txtMiddle.Enabled = txtLast.Enabled = true;
- }
- private void btnSave_Click(object sender, EventArgs e)
- {
- int result = 0;
- if (rowIndex < 0)
- {
- try
- {
- conn.Open();
- sql = @"select * from st_insert(:_firstname, :_midname, :_lastname)";
- cmd = new NpgsqlCommand(sql, conn);
- cmd.Parameters.AddWithValue("_firstname",txtFirst.Text);
- cmd.Parameters.AddWithValue("_midname", txtMiddle.Text);
- cmd.Parameters.AddWithValue("_lastname", txtLast.Text);
- result = (int)cmd.ExecuteScalar();
- conn.Close();
- if(result == 1)
- {
- MessageBox.Show("Inserted new student successfully.");
- Select();
- }
- else
- {
- MessageBox.Show("Inserted fall.");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("Insert fail. Error:"+ex.Message);
- conn.Close();
- }
- }
- else
- {
- try
- {
- conn.Open();
- sql = @"select * from st_update(:_id,:_firstname,:_midname,:_lastname)";
- cmd = new NpgsqlCommand(sql, conn);
- cmd.Parameters.AddWithValue("_id",int.Parse(dvgData.Rows[rowIndex].Cells["id"].Value.ToString())); ;
- cmd.Parameters.AddWithValue("_firstname", txtFirst.Text);
- cmd.Parameters.AddWithValue("_midname", txtMiddle.Text);
- cmd.Parameters.AddWithValue("_lastname", txtLast.Text);
- result = (int)cmd.ExecuteScalar();
- conn.Close();
- if (result == 1)
- {
- MessageBox.Show("Updated student successfully.");
- Select();
- }
- else
- {
- MessageBox.Show("Update failed.");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("Updated fail. Error:" + ex.Message);
- conn.Close();
- }
- }
- result = 0;
- txtFirst.Text = txtMiddle.Text = txtLast.Text = null;
- txtFirst.Enabled = txtMiddle.Enabled = txtLast.Enabled = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement