Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data.SqlClient;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp21
- {
- /// <summary>
- /// Used to manage a SQL Server connection and modify it's tables
- /// </summary>
- public class DBManager
- {
- /// <summary>
- /// The connection string used in the class instance
- /// </summary>
- public SqlConnection Connection { get; private set; }
- /// <summary>
- /// Initializes a DBManager instance
- /// </summary>
- /// <param name="connectionString"></param>
- public DBManager(string connectionString)
- {
- Connection = new SqlConnection(connectionString);
- }
- /// <summary>
- /// Insert values in a specified table
- /// </summary>
- /// <param name="table">Table name</param>
- /// <param name="parameters">List of parameters</param>
- public int Insert(string table, params object[] parameters)
- {
- Connection.Open();
- string[] paramList = new string[parameters.Length];
- for(int i = 0; i < parameters.Length; i++)
- {
- paramList[i] = parameters[i].ToString();
- if (paramList[i].GetType() == typeof(string))
- {
- paramList[i] = @"'" + paramList[i];
- paramList[i] += @"'";
- }
- }
- var command = new SqlCommand()
- {
- Connection = Connection,
- CommandText = $"INSERT INTO {table} VALUES({string.Join(",",paramList)})"
- };
- int affectedLines = command.ExecuteNonQuery();
- Connection.Close();
- return affectedLines;
- }
- /// <summary>
- /// Clears the specified table
- /// </summary>
- /// <param name="table">The table to be deleted</param>
- public void Delete(string table)
- {
- Connection.Open();
- SqlCommand command = new SqlCommand()
- {
- Connection = Connection,
- CommandText = $"DELETE {table}"
- };
- Connection.Close();
- }
- /// <summary>
- /// Executes a non-query command
- /// </summary>
- /// <param name="command">The command text</param>
- /// <returns>The number of affected lines</returns>
- public int NonQuery(string query)
- {
- Connection.Open();
- var result = new SqlCommand(query, Connection).ExecuteNonQuery();
- Connection.Close();
- return result;
- }
- /// <summary>
- /// Executes a scalar query
- /// </summary>
- /// <param name="query">Query text</param>
- /// <returns>The first column of the first line of the result</returns>
- public object Scalar(string query)
- {
- Connection.Open();
- var result = new SqlCommand(query, Connection).ExecuteScalar();
- Connection.Close();
- return result;
- }
- /// <summary>
- /// Executes a reader query
- /// </summary>
- /// <param name="query">Query textx</param>
- /// <returns>The SqlDataReader object, returned by the command</returns>
- public SqlDataReader Reader(string query)
- {
- Connection.Open();
- var result = new SqlCommand(query, Connection).ExecuteReader();
- return result;
- }
- }
- class Pessoa
- {
- public string Name { get; set; }
- public string CPF { get; set; }
- public int Age { get; set; }
- public Pessoa()
- {
- }
- public Pessoa(string name, string cpf, int age)
- {
- Name = name;
- CPF = cpf;
- Age = age;
- }
- public override string ToString()
- {
- return $"Nome: {Name}, CPF: {CPF}, Idade: {Age}";
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- string connStr = "Data source=localhost;Initial catalog=TestDB;Integrated security=SSPI;";
- DBManager manager = new DBManager(connStr);
- List<Pessoa> pessoas = new List<Pessoa>();
- using (SqlDataReader value = manager.Reader("SELECT nome, cpf, age FROM Pessoa"))
- {
- while (value.Read())
- {
- pessoas.Add(new Pessoa()
- {
- Name = value.GetString(0),
- CPF = value.GetString(1),
- Age = value.GetInt32(2)
- });
- }
- }
- manager.Connection.Close();
- foreach(var pessoa in pessoas)
- {
- Console.WriteLine(pessoa.ToString());
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment