Advertisement
Foxxything

sql foem

Aug 10th, 2021
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using Npgsql;
  11.  
  12. namespace psqlTest
  13. {
  14.     public partial class Form2 : Form
  15.     {
  16.         string host = global.host;
  17.         string database = global.database;
  18.         string user = global.user;
  19.         string password = global.password;
  20.  
  21.         private string connstring = string.Format("Server={0};Port={1};" +
  22.             "User Id = {2};Password={3};Database={4};",
  23.             global.host, global.port, global.user,
  24.             global.password, global.database);
  25.         private NpgsqlConnection conn;
  26.         private string sql;
  27.         private NpgsqlCommand cmd;
  28.         private DataTable dt;
  29.         private int rowIndex = -1;
  30.         public Form2()
  31.         {
  32.             InitializeComponent();
  33.         }
  34.  
  35.         private void Form2_Load(object sender, EventArgs e)
  36.         {
  37.             conn = new NpgsqlConnection(connstring);
  38.             Select();
  39.         }
  40.  
  41.         private void Select()
  42.         {
  43.             try
  44.             {
  45.                 conn.Open();
  46.                 sql = @"select * from st_select();";
  47.                 cmd = new NpgsqlCommand(sql, conn);
  48.                 dt = new DataTable();
  49.                 dt.Load(cmd.ExecuteReader());
  50.                 conn.Close();
  51.                 dvgData.DataSource = null;
  52.                 dvgData.DataSource = dt;
  53.             }
  54.             catch (Exception ex)
  55.             {
  56.                 conn.Close();
  57.                 MessageBox.Show("Error: "+ex.Message);
  58.  
  59.                 throw;
  60.             }
  61.         }
  62.  
  63.         private void dvgData_CellClick(object sender, DataGridViewCellEventArgs e)
  64.         {
  65.             if (e.RowIndex >= 0)
  66.             {
  67.                 rowIndex = e.RowIndex;
  68.                 txtFirst.Text = dvgData.Rows[e.RowIndex].Cells["firstname"].Value.ToString();
  69.                 txtMiddle.Text = dvgData.Rows[e.RowIndex].Cells["midname"].Value.ToString();
  70.                 txtLast.Text = dvgData.Rows[e.RowIndex].Cells["lastname"].Value.ToString();
  71.             }
  72.         }
  73.  
  74.         private void btnIncert_Click(object sender, EventArgs e)
  75.         {
  76.             txtFirst.Enabled = txtMiddle.Enabled = txtLast.Enabled = true;
  77.             rowIndex = -1;
  78.             txtFirst.Text = txtMiddle.Text = txtLast.Text = null;
  79.             txtFirst.Select();
  80.         }
  81.  
  82.         private void btnDelete_Click(object sender, EventArgs e)
  83.         {
  84.             if (rowIndex < 0)
  85.             {
  86.                 MessageBox.Show("Please choose student to delete");
  87.                 return;
  88.             }
  89.             try
  90.             {
  91.                 conn.Open();
  92.                 sql = @"select * from st_delete(:_id)";
  93.                 cmd = new NpgsqlCommand(sql, conn);
  94.                 cmd.Parameters.AddWithValue("_id", int.Parse(dvgData.Rows[rowIndex].Cells["id"].Value.ToString()));
  95.                 if ((int)cmd.ExecuteScalar() == 1)
  96.                 {
  97.                     MessageBox.Show("Delete student successfully");
  98.                     rowIndex = -1;
  99.                     Select();
  100.                 }
  101.                 conn.Close();
  102.             }
  103.             catch (Exception ex)
  104.             {
  105.                 conn.Close();
  106.                 MessageBox.Show("Deleted fail. Error:"+ex.Message);
  107.             }
  108.         }
  109.  
  110.         private void btnUpdate_Click(object sender, EventArgs e)
  111.         {
  112.             if(rowIndex < 0)
  113.             {
  114.                 MessageBox.Show("Please choose student to update");
  115.                 return;
  116.             }
  117.             txtFirst.Enabled = txtMiddle.Enabled = txtLast.Enabled = true;
  118.         }
  119.  
  120.         private void btnSave_Click(object sender, EventArgs e)
  121.         {
  122.             int result = 0;
  123.             if (rowIndex < 0)
  124.             {
  125.                 try
  126.                 {
  127.                     conn.Open();
  128.                     sql = @"select * from st_insert(:_firstname, :_midname, :_lastname)";
  129.                     cmd = new NpgsqlCommand(sql, conn);
  130.                     cmd.Parameters.AddWithValue("_firstname",txtFirst.Text);
  131.                     cmd.Parameters.AddWithValue("_midname", txtMiddle.Text);
  132.                     cmd.Parameters.AddWithValue("_lastname", txtLast.Text);
  133.                     result = (int)cmd.ExecuteScalar();
  134.                     conn.Close();
  135.                     if(result == 1)
  136.                     {
  137.                         MessageBox.Show("Inserted new student successfully.");
  138.                         Select();
  139.                     }
  140.                     else
  141.                     {
  142.                         MessageBox.Show("Inserted fall.");
  143.                     }
  144.                 }
  145.                 catch (Exception ex)
  146.                 {
  147.                     MessageBox.Show("Insert fail. Error:"+ex.Message);
  148.                     conn.Close();
  149.                 }
  150.             }
  151.             else
  152.             {
  153.                 try
  154.                 {
  155.                     conn.Open();
  156.                     sql = @"select * from st_update(:_id,:_firstname,:_midname,:_lastname)";
  157.                     cmd = new NpgsqlCommand(sql, conn);
  158.                     cmd.Parameters.AddWithValue("_id",int.Parse(dvgData.Rows[rowIndex].Cells["id"].Value.ToString())); ;
  159.                     cmd.Parameters.AddWithValue("_firstname", txtFirst.Text);
  160.                     cmd.Parameters.AddWithValue("_midname", txtMiddle.Text);
  161.                     cmd.Parameters.AddWithValue("_lastname", txtLast.Text);
  162.                     result = (int)cmd.ExecuteScalar();
  163.                     conn.Close();
  164.                     if (result == 1)
  165.                     {
  166.                         MessageBox.Show("Updated student successfully.");
  167.                         Select();
  168.                     }
  169.                     else
  170.                     {
  171.                         MessageBox.Show("Update failed.");
  172.                     }
  173.                 }
  174.  
  175.                 catch (Exception ex)
  176.                 {
  177.                     MessageBox.Show("Updated fail. Error:" + ex.Message);
  178.                     conn.Close();
  179.                 }
  180.  
  181.             }
  182.             result = 0;
  183.             txtFirst.Text = txtMiddle.Text = txtLast.Text = null;
  184.             txtFirst.Enabled = txtMiddle.Enabled = txtLast.Enabled = false;
  185.         }
  186.     }
  187. }
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement