Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.29 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4.  
  5. namespace DataTools
  6. {
  7.     public class DataAccess : IDisposable
  8.     {
  9.         private string conStr, server, user, pass, database;
  10.         private SqlConnection con;
  11.         private SqlDataAdapter da;
  12.         private SqlCommand com;
  13.  
  14.         /// <summary>
  15.         /// Gets or sets the server's name.
  16.         /// </summary>
  17.         public string Server
  18.         {
  19.             set
  20.             {
  21.                 server = value;
  22.                 InitializeConnection();
  23.             }
  24.             get { return server; }
  25.         }
  26.  
  27.         /// <summary>
  28.         /// Gets or sets the username to connect to the server.
  29.         /// </summary>
  30.         public string Username
  31.         {
  32.             set
  33.             {
  34.                 user = value;
  35.                 InitializeConnection();
  36.             }
  37.             get { return user; }
  38.         }
  39.  
  40.         /// <summary>
  41.         /// Gets or sets the password of the username.
  42.         /// </summary>
  43.         public string Password
  44.         {
  45.             set
  46.             {
  47.                 pass = value;
  48.                 InitializeConnection();
  49.             }
  50.             get { return pass; }
  51.         }
  52.  
  53.         /// <summary>
  54.         /// Gets or sets the current database of the connection.
  55.         /// </summary>
  56.         public string Database
  57.         {
  58.             set
  59.             {
  60.                 database = value;
  61.                 InitializeConnection();
  62.             }
  63.             get { return database; }
  64.         }
  65.  
  66.         /// <summary>
  67.         /// Initializes a new instance of DataAccess, in addition initializes the connection.
  68.         /// </summary>
  69.         /// <param name="server">SQLServer name or direction.</param>
  70.         /// <param name="user">Username to connect.</param>
  71.         /// <param name="pass">Password.</param>
  72.         /// <param name="database">Initial DataBase name.</param>
  73.         public DataAccess(string server, string user, string pass, string database)
  74.         {
  75.             this.server = server;
  76.             this.user = user;
  77.             this.pass = pass;
  78.             this.database = database;
  79.             InitializeConnection();
  80.         }
  81.  
  82.         /// <summary>
  83.         /// Opens the connection.
  84.         /// </summary>
  85.         public void OpenConnection()
  86.         {
  87.             try
  88.             {
  89.                 con.Open();
  90.             }
  91.             catch (SqlException ex)
  92.             {
  93.                 throw ex;
  94.             }
  95.         }
  96.  
  97.         /// <summary>
  98.         /// Closes the connection.
  99.         /// </summary>
  100.         public void CloseConnection()
  101.         {
  102.             try
  103.             {
  104.                 con.Close();
  105.             }
  106.             catch(SqlException ex)
  107.             {
  108.                 throw ex;
  109.             }
  110.         }
  111.  
  112.         /// <summary>
  113.         /// Executes the select statement.
  114.         /// </summary>
  115.         /// <param name="query">Query string.</param>
  116.         /// <returns>A DataTable.</returns>
  117.         public DataTable ExecuteQuery(string query)
  118.         {
  119.             DataSet ds = new DataSet();
  120.             da = new SqlDataAdapter(query, con);
  121.  
  122.             try
  123.             {
  124.                 OpenConnection();
  125.                 da.Fill(ds);
  126.                 CloseConnection();
  127.             }
  128.             catch (SqlException ex)
  129.             {
  130.                 throw ex;
  131.             }
  132.             finally
  133.             {
  134.                 da.Dispose();
  135.                 da = null;
  136.                 com.Dispose();
  137.                 com = null;
  138.             }
  139.  
  140.             return ds.Tables[0];
  141.         }
  142.  
  143.         /// <summary>
  144.         /// Executes a sql statement (insert, update, delete).
  145.         /// </summary>
  146.         /// <param name="query">The sql statement.</param>
  147.         /// <returns>The affected rows.</returns>
  148.         public int ExecuteNonQuery(string query)
  149.         {
  150.             int r = 0;
  151.             SqlCommand com = new SqlCommand(query, con);
  152.  
  153.             try
  154.             {
  155.                 r = com.ExecuteNonQuery();
  156.             }
  157.             catch (SqlException ex)
  158.             {
  159.                 throw ex;
  160.             }
  161.  
  162.             return r;
  163.         }
  164.  
  165.         /// <summary>
  166.         /// Executes an StoreProcedure with an insert, delete or update statement.
  167.         /// </summary>
  168.         /// <param name="storeProc">The StoreProcedure Object.</param>
  169.         /// <returns>The affected rows.</returns>
  170.         public int ExecuteStoreProcedureNonQuery(StoreProcedure storeProc)
  171.         {
  172.             int r = 0;
  173.             com = new SqlCommand(storeProc.Name, con);
  174.             com.CommandType = CommandType.StoredProcedure;
  175.  
  176.             try
  177.             {
  178.                 foreach (SqlParameter param in storeProc.Parameters)
  179.                 {
  180.                     com.Parameters.Add(param);
  181.                 }
  182.                 con.Open();
  183.                 r = com.ExecuteNonQuery();
  184.                 con.Close();
  185.             }
  186.             catch (SqlException ex)
  187.             {
  188.                 throw ex;
  189.             }
  190.             finally
  191.             {
  192.                 com.Dispose();
  193.                 com = null;
  194.             }
  195.  
  196.             return r;
  197.         }
  198.  
  199.         /// <summary>
  200.         /// Executes the given StoreProcedure.
  201.         /// </summary>
  202.         /// <param name="storeProc">The StoreProcedure object.</param>
  203.         /// <returns>A DataTable.</returns>
  204.         public DataTable ExecuteStoreProcedure(StoreProcedure storeProc)
  205.         {
  206.             da = new SqlDataAdapter();
  207.             DataSet ds = new DataSet();
  208.  
  209.             com = new SqlCommand(storeProc.Name, con);
  210.             com.CommandType = CommandType.StoredProcedure;
  211.  
  212.             foreach (SqlParameter param in storeProc.Parameters)
  213.                 com.Parameters.Add(param);
  214.  
  215.             da.SelectCommand = com;
  216.  
  217.             try
  218.             {
  219.                 da.Fill(ds);
  220.             }
  221.             catch (SqlException ex)
  222.             {
  223.                 throw ex;
  224.             }
  225.             finally
  226.             {
  227.                 da.Dispose();
  228.                 da = null;
  229.                 com.Dispose();
  230.                 com = null;
  231.             }
  232.  
  233.             return ds.Tables[0];
  234.         }
  235.  
  236.         // Initialize the connection
  237.         private void InitializeConnection()
  238.         {
  239.             conStr = "Server=" + server + ";UID=" + user + ";PWD=" + pass + ";Database=" + database;
  240.             con = new SqlConnection(conStr);
  241.         }
  242.  
  243.         #region IDisposable Members
  244.  
  245.         private bool disposed = false;
  246.  
  247.         public void Dispose()
  248.         {
  249.             Dispose(true);
  250.             GC.SuppressFinalize(this);
  251.         }
  252.  
  253.         protected virtual void Dispose(bool disposing)
  254.         {
  255.             if (!disposed)
  256.             {
  257.                 if (da != null)
  258.                 {
  259.                     da.Dispose();
  260.                     da = null;
  261.                 }
  262.                 if (com != null)
  263.                 {
  264.                     com.Dispose();
  265.                     com = null;
  266.                 }
  267.                 if (con != null)
  268.                 {
  269.                     con.Close();
  270.                     con.Dispose();
  271.                     con = null;
  272.                 }
  273.             }
  274.  
  275.             disposed = true;
  276.         }
  277.  
  278.         #endregion
  279.     }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement