Advertisement
seangraham44

SQL DataAccess

May 18th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Globalization;
  6. using System.Text;
  7.  
  8. namespace Common
  9. {
  10.     public class DataAccess
  11.     {
  12.         #region Private members
  13.  
  14.         private SqlDataAdapter _da;
  15.         private SqlCommand _cmd;
  16.         private string _sp_name;
  17.         private string _server_name;
  18.         private string _database_name;
  19.         private string _user_name;
  20.         private string _password;
  21.         private const string OLE_DB = "oledb";
  22.         //private bool isDisposed = false;
  23.  
  24.         #endregion
  25.  
  26.         #region Constructors
  27.  
  28.         /// <summary>
  29.         /// Default constructor, instantiates
  30.         /// a new SqlCommand object to use.
  31.         /// </summary>
  32.         public DataAccess()
  33.         {
  34.             this.CmdObject = new SqlCommand();
  35.         }
  36.  
  37.         /// <summary>
  38.         /// This constructor takes one parameter,
  39.         /// which is the name of the database
  40.         /// to connect to.
  41.         /// </summary>
  42.         /// <param name="dbName"></param>
  43.         public DataAccess(string dbName)
  44.         {
  45.             this.CmdObject = new SqlCommand();
  46.             this.Schema = dbName;
  47.         }
  48.  
  49.         public DataAccess(string schema, string server, string username, string password)
  50.         {
  51.             this.Schema = schema;
  52.             this.ServerName = server;
  53.             this.UserName = username;
  54.             this.Password = password;
  55.         }
  56.  
  57.         public DataAccess(string dbName, string commandText)
  58.         {
  59.             this.CmdObject = new SqlCommand();
  60.             this.Schema = dbName;
  61.             this.CommandText = commandText;
  62.         }
  63.  
  64.         /// <summary>
  65.         /// This constructor takes two parameters: first the name
  66.         /// of the database, then the name of a stored procedure
  67.         /// within that database.
  68.         /// </summary>
  69.         /// <param name="dbName"></param>
  70.         /// <param name="commandText"></param>
  71.         public DataAccess(string dbName, string commandText, CommandType commandType)
  72.         {
  73.             this.CmdObject = new SqlCommand();
  74.             this.Schema = dbName;
  75.             this.CommandText = commandText;
  76.             this.CmdType = commandType;
  77.         }
  78.  
  79.         #endregion
  80.  
  81.  
  82.         public void DBOps(string sql)
  83.         {
  84.             DBOps(sql, CommandType.Text, null);
  85.         }
  86.  
  87.         public void DBOps(string sql, CommandType commandtype, List<SqlParameter> dbparams)
  88.         {
  89.             SqlConnection conn = new SqlConnection();
  90.             conn.ConnectionString = ConnectionString(this.Schema, this.ServerName, this.UserName, this.Password);
  91.             conn.Open();
  92.             SqlCommand cmd = new SqlCommand(sql, conn);
  93.             cmd.CommandType = commandtype;
  94.  
  95.             if (dbparams != null)
  96.             {
  97.                 foreach (SqlParameter sq in dbparams)
  98.                 {
  99.                     cmd.Parameters.Add(sq);
  100.                 }
  101.             }
  102.  
  103.  
  104.             cmd.ExecuteNonQuery();
  105.         }
  106.  
  107.         public DataTable FetchData(string sql)
  108.         {
  109.             return FetchData(sql, CommandType.Text, null);
  110.         }
  111.  
  112.         public DataTable FetchData(string sql, CommandType commandtype, List<SqlParameter> dbparams)
  113.         {
  114.             if (!string.IsNullOrEmpty(sql))
  115.             {
  116.                 SqlConnection conn = new SqlConnection();
  117.                 conn.ConnectionString = ConnectionString(this.Schema, this.ServerName, this.UserName, this.Password);
  118.                 conn.Open();
  119.  
  120.                 SqlCommand cmd = new SqlCommand();
  121.  
  122.                 cmd.CommandType = commandtype;
  123.  
  124.                 if (dbparams != null)
  125.                 {
  126.                     foreach (SqlParameter param in dbparams)
  127.                     {
  128.                         cmd.Parameters.Add(param);
  129.                     }
  130.                 }
  131.  
  132.                 cmd.CommandText = sql;
  133.                 cmd.Connection = conn;
  134.                 SqlDataReader rdr;
  135.                 rdr = cmd.ExecuteReader();
  136.                 DataTable dt = new DataTable();
  137.                 dt.Load(rdr);
  138.                 conn.Close();
  139.                 rdr.Close();
  140.                 return dt;
  141.             }
  142.             else
  143.             {
  144.                 return null;
  145.             }
  146.         }
  147.  
  148.         #region Private Methods
  149.  
  150.  
  151.         private static string ConnectionString(string schema, string server, string user, string password)
  152.         {
  153.             string conn = string.Format("Server={0};Database={1};User Id={2};Password={3};", server, schema, user, password);
  154.             return conn ;
  155.         }
  156.  
  157.         private DataSet GetDataSet(bool useSqlClient)
  158.         {
  159.             ///summ creates a new DataSet
  160.             using (DataSet _ds = new DataSet())
  161.             {
  162.                 _ds.Locale = CultureInfo.InvariantCulture;
  163.  
  164.                 using (this.CmdObject)
  165.                 {
  166.                     this.CmdObject.Connection = DataAccess.GetSqlServerConnection(
  167.                         this.ServerName, this.Schema,
  168.                         useSqlClient ? "sqlclient" : "oledb", this.UserName, this.Password);
  169.  
  170.                     this.CmdObject.CommandText = this.CommandText;
  171.                     this.CmdObject.CommandType = this.CmdType;
  172.  
  173.                     // create new SqlDataAdapter and fill DataSet
  174.                     using (_da = new SqlDataAdapter(this.CmdObject))
  175.                     {
  176.                         // fill the DataSet
  177.                         _da.Fill(_ds, "recordset");
  178.  
  179.                         // create new DataView from the resultant table
  180.                         return _ds;
  181.                     }
  182.                 }
  183.             }
  184.         }
  185.  
  186.         private static SqlConnection GetSqlServerConnection(string schema, string server, string provider, string username, string password)
  187.         {
  188.             StringBuilder _connstring = new StringBuilder(string.Empty);
  189.  
  190.             switch (provider.ToLower())
  191.             {
  192.                 case "sqlclient":
  193.                     _connstring.Append(string.Format("Data Source={0};Initial Catalog={1};" +
  194.                         "User Id={2};Password={3};", server, schema, username, password));
  195.                     break;
  196.                 case OLE_DB:
  197.                     _connstring.Append(string.Format("Provider=sqloledb;Data Source={0}" +
  198.                         ";Initial Catalog={1};User Id={2}Password={3};", server, schema, username, password));
  199.                     break;
  200.  
  201.                 default:
  202.                     throw new Exception("Invalid provider string specified");
  203.             }
  204.  
  205.             return new SqlConnection(_connstring.ToString());
  206.         }
  207.  
  208.         private DataSet GetDataSet()
  209.         {
  210.             return this.GetDataSet(true);
  211.         }
  212.  
  213.         public object MyDataObject
  214.         {
  215.             get
  216.             {
  217.                 using (DataSet _ds = this.GetDataSet())
  218.                 {
  219.                     // create new DataView from the resultant table
  220.                     return (Object)(_ds.Tables["recordset"]);
  221.                 }
  222.             }
  223.         }
  224.  
  225.         public DataView MyDataView
  226.         {
  227.             get
  228.             {
  229.                 using (DataSet _ds = this.GetDataSet())
  230.                 {
  231.                     // create new DataView from the resultant table
  232.                     return new DataView(_ds.Tables["recordset"]);
  233.                 }
  234.             }
  235.         }
  236.  
  237.         public DataTable MyDataTable
  238.         {
  239.             get
  240.             {
  241.                 using (DataSet _ds = this.GetDataSet())
  242.                 {
  243.                     // create new DataView from the resultant table
  244.                     return _ds.Tables["recordset"] as DataTable;
  245.                 }
  246.             }
  247.         }
  248.  
  249.         private string ServerName
  250.         {
  251.             get { return _server_name; }
  252.             set { _server_name = value; }
  253.         }
  254.  
  255.         private string UserName
  256.         {
  257.             get { return _user_name; }
  258.             set { _user_name = value; }
  259.         }
  260.  
  261.         public string Password
  262.         {
  263.             get { return _password; }
  264.             set { _password = value; }
  265.         }
  266.  
  267.         public string Schema
  268.         {
  269.             get { return _database_name; }
  270.             set { _database_name = value; }
  271.         }
  272.  
  273.         public string CommandText
  274.         {
  275.             // stored procedure name property accessor methods
  276.             get { return _sp_name; }
  277.  
  278.             set { _sp_name = value; }
  279.         }
  280.  
  281.         public CommandType CmdType
  282.         {
  283.             get;
  284.             set;
  285.         }
  286.  
  287.         public SqlCommand CmdObject
  288.         {
  289.             get { return _cmd; }
  290.             set { _cmd = value; }
  291.         }
  292.  
  293.         #endregion
  294.  
  295.         #region IDisposable method implementation
  296.         protected void Dispose(bool disposing)
  297.         {
  298.             if (disposing)
  299.             {
  300.                 // Code to dispose the managed resources of the class
  301.             }
  302.             // Code to dispose the un-managed resources of the class
  303.  
  304.             //isDisposed = true;
  305.         }
  306.  
  307.         public void Dispose()
  308.         {
  309.             Dispose(true);
  310.             GC.SuppressFinalize(this);
  311.         }
  312.         #endregion
  313.  
  314.     }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement