Stacklysm

test

Nov 6th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.05 KB | None | 0 0
  1. using System;
  2. using System.Data.SqlClient;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp21
  9. {
  10.     /// <summary>
  11.     /// Used to manage a SQL Server connection and modify it's tables
  12.     /// </summary>
  13.     public class DBManager
  14.     {
  15.         /// <summary>
  16.         /// The connection string used in the class instance
  17.         /// </summary>
  18.         public SqlConnection Connection { get; private set; }
  19.  
  20.         /// <summary>
  21.         /// Initializes a DBManager instance
  22.         /// </summary>
  23.         /// <param name="connectionString"></param>
  24.         public DBManager(string connectionString)
  25.         {
  26.             Connection = new SqlConnection(connectionString);
  27.         }
  28.  
  29.         /// <summary>
  30.         /// Insert values in a specified table
  31.         /// </summary>
  32.         /// <param name="table">Table name</param>
  33.         /// <param name="parameters">List of parameters</param>
  34.         public int Insert(string table, params object[] parameters)
  35.         {
  36.             Connection.Open();
  37.  
  38.             string[] paramList = new string[parameters.Length];
  39.  
  40.             for(int i = 0; i < parameters.Length; i++)
  41.             {
  42.                 paramList[i] = parameters[i].ToString();
  43.  
  44.                 if (paramList[i].GetType() == typeof(string))
  45.                 {
  46.                     paramList[i] = @"'" + paramList[i];
  47.                     paramList[i] += @"'";
  48.                 }
  49.             }
  50.  
  51.             var command = new SqlCommand()
  52.             {
  53.                 Connection = Connection,
  54.                 CommandText = $"INSERT INTO {table} VALUES({string.Join(",",paramList)})"
  55.             };
  56.  
  57.             int affectedLines = command.ExecuteNonQuery();
  58.             Connection.Close();
  59.  
  60.             return affectedLines;
  61.         }
  62.        
  63.         /// <summary>
  64.         /// Clears the specified table
  65.         /// </summary>
  66.         /// <param name="table">The table to be deleted</param>
  67.         public void Delete(string table)
  68.         {
  69.             Connection.Open();
  70.  
  71.             SqlCommand command = new SqlCommand()
  72.             {
  73.                 Connection = Connection,
  74.                 CommandText = $"DELETE {table}"
  75.             };
  76.  
  77.             Connection.Close();
  78.         }
  79.  
  80.         /// <summary>
  81.         /// Executes a non-query command
  82.         /// </summary>
  83.         /// <param name="command">The command text</param>
  84.         /// <returns>The number of affected lines</returns>
  85.         public int NonQuery(string query)
  86.         {
  87.             Connection.Open();
  88.             var result = new SqlCommand(query, Connection).ExecuteNonQuery();
  89.             Connection.Close();
  90.             return result;
  91.         }
  92.  
  93.         /// <summary>
  94.         /// Executes a scalar query
  95.         /// </summary>
  96.         /// <param name="query">Query text</param>
  97.         /// <returns>The first column of the first line of the result</returns>
  98.         public object Scalar(string query)
  99.         {
  100.             Connection.Open();
  101.             var result = new SqlCommand(query, Connection).ExecuteScalar();
  102.             Connection.Close();
  103.             return result;
  104.         }
  105.  
  106.         /// <summary>
  107.         /// Executes a reader query
  108.         /// </summary>
  109.         /// <param name="query">Query textx</param>
  110.         /// <returns>The SqlDataReader object, returned by the command</returns>
  111.         public SqlDataReader Reader(string query)
  112.         {
  113.             Connection.Open();
  114.             var result = new SqlCommand(query, Connection).ExecuteReader();
  115.             return result;
  116.         }
  117.     }
  118.  
  119.     class Pessoa
  120.     {
  121.         public string Name { get; set; }
  122.         public string CPF { get; set; }
  123.         public int Age { get; set; }
  124.  
  125.         public Pessoa()
  126.         {
  127.  
  128.         }
  129.  
  130.         public Pessoa(string name, string cpf, int age)
  131.         {
  132.             Name = name;
  133.             CPF = cpf;
  134.             Age = age;
  135.         }
  136.  
  137.         public override string ToString()
  138.         {
  139.             return $"Nome: {Name}, CPF: {CPF}, Idade: {Age}";
  140.         }
  141.     }
  142.  
  143.     class Program
  144.     {
  145.         static void Main(string[] args)
  146.         {
  147.             string connStr = "Data source=localhost;Initial catalog=TestDB;Integrated security=SSPI;";
  148.  
  149.             DBManager manager = new DBManager(connStr);
  150.            
  151.             List<Pessoa> pessoas = new List<Pessoa>();
  152.  
  153.             using (SqlDataReader value = manager.Reader("SELECT nome, cpf, age FROM Pessoa"))
  154.             {
  155.                 while (value.Read())
  156.                 {
  157.                     pessoas.Add(new Pessoa()
  158.                     {
  159.                         Name = value.GetString(0),
  160.                         CPF = value.GetString(1),
  161.                         Age = value.GetInt32(2)
  162.                     });
  163.                 }
  164.             }
  165.  
  166.             manager.Connection.Close();
  167.  
  168.             foreach(var pessoa in pessoas)
  169.             {
  170.                 Console.WriteLine(pessoa.ToString());
  171.             }
  172.  
  173.             Console.ReadKey();
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment