Advertisement
DimcheDim

PgSQL

May 22nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data;
  7. using Npgsql;
  8. using System.Windows.Forms;
  9.  
  10. namespace student_progress
  11. {
  12.     class PgSQL
  13.     {
  14.        
  15.         string connParam = null;
  16.  
  17.         public PgSQL(string connectionString)
  18.         {
  19.             connParam = connectionString;
  20.         }
  21.  
  22.         public DataTable OutTable(string query)
  23.         {
  24.             NpgsqlConnection dbConnection = null;
  25.             NpgsqlCommand command = null;
  26.             DataTable dt = new DataTable();
  27.             try
  28.             {
  29.                 dbConnection = new NpgsqlConnection(connParam);
  30.                 dbConnection.Open();
  31.                 command = new NpgsqlCommand();
  32.                 command.Connection = dbConnection;
  33.                 command.CommandType = CommandType.StoredProcedure;
  34.                 command.CommandText = query;
  35.                 NpgsqlDataAdapter dbDataAdapter = new NpgsqlDataAdapter(command);
  36.                 DataSet ds = new DataSet();
  37.                 dbDataAdapter.Fill(ds);
  38.                 dt = ds.Tables[0];
  39.             }
  40.             catch (NpgsqlException p)
  41.             {
  42.                 throw new Exception (p.Message);
  43.             }
  44.             finally
  45.             {
  46.                 dbConnection.Close();
  47.             }
  48.             return dt;
  49.         }
  50.  
  51.         public void Del(string procName, int id)
  52.         {
  53.             NpgsqlConnection dbConnection = null;
  54.             NpgsqlCommand command = null;
  55.             try
  56.             {
  57.                 dbConnection = new NpgsqlConnection(connParam);
  58.                 command = new NpgsqlCommand();
  59.                 command.Connection = dbConnection;
  60.                 command.CommandType = CommandType.StoredProcedure;
  61.                 command.CommandText = procName;
  62.  
  63.                 command.Parameters.AddWithValue("i", id);
  64.  
  65.                 dbConnection.Open();
  66.                 command.Prepare();
  67.                 command.ExecuteNonQuery();
  68.             }
  69.             catch (Exception p)
  70.             {
  71.                 throw new Exception(p.Message);
  72.             }
  73.             finally
  74.             {
  75.                 dbConnection.Close();
  76.             }
  77.         }
  78.  
  79.         public void AddEditStat(string procName, int st, int tch, int d, string r, string dt, int tr, int gr, string att, int id = -1)
  80.         {
  81.             NpgsqlConnection dbConnection = null;
  82.             NpgsqlCommand command = null;
  83.             try
  84.             {
  85.                 dbConnection = new NpgsqlConnection(connParam);
  86.                 command = new NpgsqlCommand();
  87.                 command.Connection = dbConnection;
  88.                 command.CommandType = CommandType.StoredProcedure;
  89.                 command.CommandText = procName;
  90.  
  91.                 command.Parameters.AddWithValue("st", st);
  92.                 command.Parameters.AddWithValue("tch", tch);
  93.                 command.Parameters.AddWithValue("d", d);
  94.                 command.Parameters.AddWithValue("r", NpgsqlTypes.NpgsqlDbType.Varchar, r);
  95.                 command.Parameters.AddWithValue("dt", NpgsqlTypes.NpgsqlDbType.Date, Convert.ToDateTime(dt));
  96.                 command.Parameters.AddWithValue("tr", tr);
  97.                 command.Parameters.AddWithValue("gr", gr);
  98.                 command.Parameters.AddWithValue("att", NpgsqlTypes.NpgsqlDbType.Varchar, att);
  99.                 if (id != -1)
  100.                     command.Parameters.AddWithValue("i", id);
  101.  
  102.                 dbConnection.Open();
  103.                 command.Prepare();
  104.                 command.ExecuteNonQuery();
  105.             }
  106.             catch (Exception p)
  107.             {
  108.                 throw new Exception(p.Message);
  109.             }
  110.             finally
  111.             {
  112.                 dbConnection.Close();
  113.             }
  114.         }
  115.  
  116.         public void AddEditStudent(string procName, string n, string ph, string ay, int f, int gr, string rb, int id = -1)
  117.         {
  118.             NpgsqlConnection dbConnection = null;
  119.             NpgsqlCommand command = null;
  120.             try
  121.             {
  122.                 dbConnection = new NpgsqlConnection(connParam);
  123.                 command = new NpgsqlCommand();
  124.                 command.Connection = dbConnection;
  125.                 command.CommandType = CommandType.StoredProcedure;
  126.                 command.CommandText = procName;
  127.  
  128.                 command.Parameters.AddWithValue("n", NpgsqlTypes.NpgsqlDbType.Varchar, n);
  129.                 command.Parameters.AddWithValue("ph", NpgsqlTypes.NpgsqlDbType.Varchar, ph);
  130.                 command.Parameters.AddWithValue("ay", NpgsqlTypes.NpgsqlDbType.Varchar, ay);
  131.                 command.Parameters.AddWithValue("f", NpgsqlTypes.NpgsqlDbType.Integer, f);
  132.                 command.Parameters.AddWithValue("gr", NpgsqlTypes.NpgsqlDbType.Integer, gr);
  133.                 command.Parameters.AddWithValue("rb", NpgsqlTypes.NpgsqlDbType.Varchar, rb);
  134.                 if (id != -1)
  135.                     command.Parameters.AddWithValue("i", id);
  136.  
  137.                 dbConnection.Open();
  138.                 command.Prepare();
  139.                 command.ExecuteNonQuery();
  140.             }
  141.             catch (Exception p)
  142.             {
  143.                 throw new Exception(p.Message);
  144.             }
  145.             finally
  146.             {
  147.                 dbConnection.Close();
  148.             }
  149.         }
  150.  
  151.         public void AddEditDisc(string procName, string n, int id = -1)
  152.         {
  153.             NpgsqlConnection dbConnection = null;
  154.             NpgsqlCommand command = null;
  155.             try
  156.             {
  157.                 dbConnection = new NpgsqlConnection(connParam);
  158.                 command = new NpgsqlCommand();
  159.                 command.Connection = dbConnection;
  160.                 command.CommandType = CommandType.StoredProcedure;
  161.                 command.CommandText = procName;
  162.  
  163.                 command.Parameters.AddWithValue("n", NpgsqlTypes.NpgsqlDbType.Varchar, n);
  164.                 if (id != -1)
  165.                     command.Parameters.AddWithValue("i", id);
  166.  
  167.                 dbConnection.Open();
  168.                 command.Prepare();
  169.                 command.ExecuteNonQuery();
  170.             }
  171.             catch (Exception p)
  172.             {
  173.                 throw new Exception(p.Message);
  174.             }
  175.             finally
  176.             {
  177.                 dbConnection.Close();
  178.             }
  179.         }
  180.  
  181.         public void AddEditTeacher(string procName, string n, string ph, int ct, int id = -1)
  182.         {
  183.             NpgsqlConnection dbConnection = null;
  184.             NpgsqlCommand command = null;
  185.             try
  186.             {
  187.                 dbConnection = new NpgsqlConnection(connParam);
  188.                 command = new NpgsqlCommand();
  189.                 command.Connection = dbConnection;
  190.                 command.CommandType = CommandType.StoredProcedure;
  191.                 command.CommandText = procName;
  192.  
  193.                 command.Parameters.AddWithValue("n", NpgsqlTypes.NpgsqlDbType.Varchar, n);
  194.                 command.Parameters.AddWithValue("ph", NpgsqlTypes.NpgsqlDbType.Varchar, ph);
  195.                 command.Parameters.AddWithValue("ct", NpgsqlTypes.NpgsqlDbType.Integer, ct);
  196.                 if (id != -1)
  197.                     command.Parameters.AddWithValue("i", id);
  198.  
  199.                 dbConnection.Open();
  200.                 command.Prepare();
  201.                 command.ExecuteNonQuery();
  202.             }
  203.             catch (Exception p)
  204.             {
  205.                 throw new Exception(p.Message);
  206.             }
  207.             finally
  208.             {
  209.                 dbConnection.Close();
  210.             }
  211.         }
  212.  
  213.         public void AddEditFaculty(string procName, string n, int id = -1)
  214.         {
  215.             NpgsqlConnection dbConnection = null;
  216.             NpgsqlCommand command = null;
  217.             try
  218.             {
  219.                 dbConnection = new NpgsqlConnection(connParam);
  220.                 command = new NpgsqlCommand();
  221.                 command.Connection = dbConnection;
  222.                 command.CommandType = CommandType.StoredProcedure;
  223.                 command.CommandText = procName;
  224.  
  225.                 command.Parameters.AddWithValue("n", NpgsqlTypes.NpgsqlDbType.Varchar, n);
  226.                 if (id != -1)
  227.                     command.Parameters.AddWithValue("i", id);
  228.  
  229.                 dbConnection.Open();
  230.                 command.Prepare();
  231.                 command.ExecuteNonQuery();
  232.             }
  233.             catch (Exception p)
  234.             {
  235.                 throw new Exception(p.Message);
  236.             }
  237.             finally
  238.             {
  239.                 dbConnection.Close();
  240.             }
  241.         }
  242.  
  243.         public void AddEditCathedra(string procName, string n, int f, string ph, string mrg, int id = -1)
  244.         {
  245.             NpgsqlConnection dbConnection = null;
  246.             NpgsqlCommand command = null;
  247.             try
  248.             {
  249.                 dbConnection = new NpgsqlConnection(connParam);
  250.                 command = new NpgsqlCommand();
  251.                 command.Connection = dbConnection;
  252.                 command.CommandType = CommandType.StoredProcedure;
  253.                 command.CommandText = procName;
  254.  
  255.                 command.Parameters.AddWithValue("n", NpgsqlTypes.NpgsqlDbType.Varchar, n);
  256.                 command.Parameters.AddWithValue("f", NpgsqlTypes.NpgsqlDbType.Integer, f);
  257.                 command.Parameters.AddWithValue("ph", NpgsqlTypes.NpgsqlDbType.Varchar, ph);
  258.                 command.Parameters.AddWithValue("mrg", NpgsqlTypes.NpgsqlDbType.Varchar, mrg);
  259.                 if (id != -1)
  260.                     command.Parameters.AddWithValue("i", id);
  261.  
  262.                 dbConnection.Open();
  263.                 command.Prepare();
  264.                 command.ExecuteNonQuery();
  265.             }
  266.             catch (Exception p)
  267.             {
  268.                 throw new Exception(p.Message);
  269.             }
  270.             finally
  271.             {
  272.                 dbConnection.Close();
  273.             }
  274.         }
  275.  
  276.         public void AddEditSpec(string procName, string n, string cd, int ct, int id = -1)
  277.         {
  278.             NpgsqlConnection dbConnection = null;
  279.             NpgsqlCommand command = null;
  280.             try
  281.             {
  282.                 dbConnection = new NpgsqlConnection(connParam);
  283.                 command = new NpgsqlCommand();
  284.                 command.Connection = dbConnection;
  285.                 command.CommandType = CommandType.StoredProcedure;
  286.                 command.CommandText = procName;
  287.  
  288.                 command.Parameters.AddWithValue("n", NpgsqlTypes.NpgsqlDbType.Varchar, n);
  289.                 command.Parameters.AddWithValue("cd", NpgsqlTypes.NpgsqlDbType.Varchar, cd);
  290.                 command.Parameters.AddWithValue("ct", NpgsqlTypes.NpgsqlDbType.Integer, ct);
  291.                 if (id != -1)
  292.                     command.Parameters.AddWithValue("i", id);
  293.  
  294.                 dbConnection.Open();
  295.                 command.Prepare();
  296.                 command.ExecuteNonQuery();
  297.             }
  298.             catch (Exception p)
  299.             {
  300.                 throw new Exception(p.Message);
  301.             }
  302.             finally
  303.             {
  304.                 dbConnection.Close();
  305.             }
  306.         }
  307.  
  308.         public void AddEditGroup(string procName, string grn, int sp, string hm, int ppl, int id = -1)
  309.         {
  310.             NpgsqlConnection dbConnection = null;
  311.             NpgsqlCommand command = null;
  312.             try
  313.             {
  314.                 dbConnection = new NpgsqlConnection(connParam);
  315.                 command = new NpgsqlCommand();
  316.                 command.Connection = dbConnection;
  317.                 command.CommandType = CommandType.StoredProcedure;
  318.                 command.CommandText = procName;
  319.  
  320.                 command.Parameters.AddWithValue("grn", NpgsqlTypes.NpgsqlDbType.Varchar, grn);
  321.                 command.Parameters.AddWithValue("sp", NpgsqlTypes.NpgsqlDbType.Integer, sp);
  322.                 command.Parameters.AddWithValue("hm", NpgsqlTypes.NpgsqlDbType.Varchar, hm);
  323.                 command.Parameters.AddWithValue("ppl", NpgsqlTypes.NpgsqlDbType.Integer, ppl);
  324.                 if (id != -1)
  325.                     command.Parameters.AddWithValue("i", id);
  326.  
  327.                 dbConnection.Open();
  328.                 command.Prepare();
  329.                 command.ExecuteNonQuery();
  330.             }
  331.             catch (Exception p)
  332.             {
  333.                 throw new Exception(p.Message);
  334.             }
  335.             finally
  336.             {
  337.                 dbConnection.Close();
  338.             }
  339.         }
  340.     }
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement