Advertisement
Kelvin1337

SQL

Dec 16th, 2020
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.62 KB | None | 0 0
  1. namespace UnixEvents
  2. {
  3.     using System;
  4.     using System.Data.SqlClient;
  5.     using System.Data;
  6.     using System.Threading;
  7.     using System.Windows.Forms;
  8.     using System.Diagnostics;
  9.     using System.Collections.Generic;
  10.  
  11.     class SQL
  12.     {
  13.         private static SqlConnection sqlconn;
  14.  
  15.  
  16.  
  17.         public static bool ExecuteQuery(string Query, SqlConnection con)
  18.         {
  19.  
  20.             using (con)
  21.             {
  22.                 try
  23.                 {
  24.                     SqlCommand cmd = new SqlCommand(Query, con);
  25.                     con.Open();
  26.                     cmd.ExecuteNonQuery();
  27.                     con.Close();
  28.                     return true;
  29.                 }
  30.                 catch (Exception ex)
  31.                 {
  32.                     Log.UpdateLogs(
  33.                         LoggerLevel.LogLevel.Error, "MSSQL Error -> Function ExecuteQuery(" + Query + "), exception catched: " + ex.Message);
  34.                    // MessageBox.Show(ex.Message);
  35.                     return false;
  36.                 }
  37.             }
  38.  
  39.         }
  40.         public static uint ReadUint(string Query, SqlConnection con)
  41.         {
  42.             uint Result = 0;
  43.             using (con)
  44.             {
  45.                 try
  46.                 {
  47.                     SqlCommand cmd = new SqlCommand(Query, con);
  48.                     con.Open();
  49.                     cmd.CommandType = CommandType.Text;
  50.                     Result = Convert.ToUInt32(cmd.ExecuteScalar());
  51.  
  52.  
  53.                     con.Close();
  54.  
  55.                 }
  56.                 catch (Exception ex)
  57.                 {
  58.                     Log.UpdateLogs(
  59.                         LoggerLevel.LogLevel.Error, "MSSQL Error -> Function ReadIntValue(" + Query + "), exception catched: " + ex.Message);
  60.  
  61.  
  62.                 }
  63.             }
  64.             return Result;
  65.         }
  66.  
  67.         public static int ReadInt(string Query, SqlConnection con)
  68.         {
  69.             int Result = 0;
  70.             using (con)
  71.             {
  72.                 try
  73.                 {
  74.                     SqlCommand cmd = new SqlCommand(Query, con);
  75.                     con.Open();
  76.                     cmd.CommandType = CommandType.Text;
  77.                     Result = Convert.ToInt32(cmd.ExecuteScalar());
  78.  
  79.  
  80.                     con.Close();
  81.  
  82.                 }
  83.                 catch (Exception ex)
  84.                 {
  85.                     Log.UpdateLogs(
  86.                         LoggerLevel.LogLevel.Error, "MSSQL Error -> Function ReadIntValue(" + Query + "), exception catched: " + ex.Message);
  87.  
  88.  
  89.                 }
  90.             }
  91.             return Result;
  92.         }
  93.         public static ulong ReadUlong(string Query, SqlConnection con)
  94.         {
  95.             ulong Result = 0;
  96.             using (con)
  97.             {
  98.                 try
  99.                 {
  100.                     SqlCommand cmd = new SqlCommand(Query, con);
  101.                     con.Open();
  102.                     cmd.CommandType = CommandType.Text;
  103.                     Result = Convert.ToUInt64(cmd.ExecuteScalar());
  104.  
  105.  
  106.                     con.Close();
  107.  
  108.                 }
  109.                 catch (Exception ex)
  110.                 {
  111.                     Log.UpdateLogs(
  112.                         LoggerLevel.LogLevel.Error, "MSSQL Error -> Function ReadUlongValue(" + Query + "), exception catched: " + ex.Message);
  113.  
  114.  
  115.                 }
  116.             }
  117.             return Result;
  118.         }
  119.         public static string ReadString(string Query, SqlConnection con)
  120.         {
  121.             string Result = "";
  122.             using (con)
  123.             {
  124.                 try
  125.                 {
  126.                     SqlCommand cmd = new SqlCommand(Query, con);
  127.                     con.Open();
  128.                     cmd.CommandType = CommandType.Text;
  129.                     Result = (string)cmd.ExecuteScalar();
  130.  
  131.  
  132.                     con.Close();
  133.  
  134.                 }
  135.                 catch (Exception ex)
  136.                 {
  137.                     Log.UpdateLogs(
  138.                         LoggerLevel.LogLevel.Error, "MSSQL Error -> Function ReadString(" + Query + "), exception catched: " + ex.Message);
  139.  
  140.  
  141.                 }
  142.             }
  143.             return Result;
  144.         }
  145.  
  146.         public static string[] getSingleArrayData(string Query, SqlConnection con)
  147.         {
  148.             try
  149.             {
  150.                 SqlDataAdapter SqlAD = new SqlDataAdapter();
  151.                 SqlAD.SelectCommand = new SqlCommand(Query, con);
  152.                 con.Open();
  153.                 DataSet ds = new DataSet();
  154.                 SqlAD.Fill(ds);
  155.  
  156.                 DataTable dt = ds.Tables[0];
  157.  
  158.                 if (dt.Rows.Count != 0)
  159.                 {
  160.                     string[] arr = new string[dt.Rows[0].ItemArray.Length];
  161.  
  162.                     arrayInit.InitStringArray(ref arr);
  163.  
  164.  
  165.                     DataRow row = dt.Rows[0]; //first array
  166.  
  167.                     for (int i = 0; i < dt.Rows[0].ItemArray.Length; i++)
  168.                     {
  169.                         arr[i] = row[i].ToString();
  170.  
  171.                     }
  172.                     return arr;
  173.  
  174.                 }
  175.                 con.Close();
  176.             }
  177.  
  178.             catch (Exception ex)
  179.             {
  180.  
  181.                 Log.UpdateLogs(
  182.                           LoggerLevel.LogLevel.Error, "MSSQL Error -> Function getStringArray(" + Query + "), exception catched: " + ex.Message);
  183.  
  184.             }
  185.             return null;
  186.         }
  187.  
  188.         public static string[] getColumnRowsData(string Query, SqlConnection con)
  189.         {
  190.             try
  191.             {
  192.                 SqlDataAdapter SqlAD = new SqlDataAdapter();
  193.                 SqlAD.SelectCommand = new SqlCommand(Query, con);
  194.  
  195.                 DataSet ds = new DataSet();
  196.                 SqlAD.Fill(ds);
  197.  
  198.                 DataTable dt = ds.Tables[0];
  199.                 string[] arr = new string[dt.Rows.Count];
  200.  
  201.                 int i = 0;
  202.                 foreach (DataRow row in dt.Rows)
  203.                 {
  204.                     //MessageBox.Show(row[0].ToString());
  205.                     arr[i] = row[0].ToString();
  206.                     i++;
  207.                 }
  208.  
  209.                 return arr;
  210.  
  211.             }
  212.             catch (Exception ex)
  213.             {
  214.                 Log.UpdateLogs(
  215.                           LoggerLevel.LogLevel.Error, "MSSQL Error -> Function getStringArray(" + Query + "), exception catched:" + ex.Message);
  216.  
  217.  
  218.             }
  219.             return null;
  220.         }
  221.  
  222.         public static IEnumerable<uint> getIDs(string query, string colname, SqlConnection con)
  223.         {
  224.  
  225.  
  226.             using (con)
  227.             {
  228.                 con.Open();
  229.                 using (SqlCommand cmd = new SqlCommand(query, con))
  230.                 {
  231.                     using (SqlDataReader reader = cmd.ExecuteReader())
  232.                     {
  233.                         while (reader.Read())
  234.                         {
  235.                             yield return Convert.ToUInt32(reader.GetInt32(reader.GetOrdinal(colname)));
  236.                         }
  237.                     }
  238.                 }
  239.             }
  240.  
  241.  
  242.  
  243.         }
  244.  
  245.  
  246.         public static SqlConnection CurrentConnection(string Database)
  247.         {
  248.             string ConString = "Data Source=" + SQLSettings.SqlServername + ";initial catalog=" + Database + ";user id=" + SQLSettings.SqlUsername + ";password=" + SQLSettings.SqlPassword;
  249.             sqlconn = new SqlConnection(ConString);
  250.             return sqlconn;
  251.         }
  252.         public static string GetConnectionString(string Database)
  253.         {
  254.             string ConString = "Data Source=" + SQLSettings.SqlServername + ";initial catalog=" + Database + ";user id=" + SQLSettings.SqlUsername + ";password=" + SQLSettings.SqlPassword;
  255.             sqlconn = new SqlConnection(ConString);
  256.             return ConString;
  257.         }
  258.  
  259.         public static bool HaveConnection(SqlConnection con)
  260.         {
  261.  
  262.             using (con)
  263.             {
  264.                 try
  265.                 {
  266.                     //Thread.Sleep(1000);
  267.                     con.Open();
  268.  
  269.                     //Log.UpdateLogs(
  270.                     //               LoggerLevel.LogLevel.Notify, $"{con.Database.ToString()} has been initialized successufly");
  271.  
  272.                     MessageBox.Show($"{con.Database.ToString()} has been initialized successufly");
  273.                     con.Close();
  274.                     return true;
  275.                 }
  276.                 catch (Exception ex)
  277.                 {
  278.                     Log.UpdateLogs(
  279.                         LoggerLevel.LogLevel.Error, "MSSQL Error -> exception catched: " + ex.Message);
  280.                     return false;
  281.  
  282.  
  283.                 }
  284.  
  285.  
  286.             }
  287.  
  288.  
  289.         }
  290.         public static bool IsConnected()
  291.         {
  292.             try
  293.             {
  294.                 if (sqlconn.State == ConnectionState.Open)
  295.                 {
  296.                     return true;
  297.                 }
  298.             }
  299.             catch (Exception ex)
  300.             {
  301.  
  302.                 Log.UpdateLogs(
  303.                     LoggerLevel.LogLevel.Error, ex.Message);
  304.                 return false;
  305.             }
  306.             return false;
  307.         }
  308.     }
  309.     class arrayInit
  310.     {
  311.         public static void InitStringArray(ref string[] arr)
  312.         {
  313.             for (int i = 0; i < arr.Length; i++)
  314.             {
  315.                 arr[i] = "";
  316.             }
  317.         }
  318.  
  319.         public static void InitIntArray(ref int[] arr)
  320.         {
  321.             for (int i = 0; i < arr.Length; i++)
  322.             {
  323.                 arr[i] = 0;
  324.             }
  325.         }
  326.  
  327.         public static void InitBoolArray(ref bool[] arr)
  328.         {
  329.             for (int i = 0; i < arr.Length; i++)
  330.             {
  331.                 arr[i] = false;
  332.             }
  333.         }
  334.     }
  335. }
  336.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement