Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » Odbc

Public general use code classes and xml files that we've compiled and used over the years:

ODBC support class.

   1:  using System;
   2:  using System.Web;
   3:  using System.Xml;
   4:  using System.Configuration;
   5:  using System.Data;
   6:  using System.Data.Odbc;
   7:  using System.Collections;
   8:  using System.Text;
   9:   
  10:  namespace Ia.Cl.Model.Db
  11:  {
  12:      ////////////////////////////////////////////////////////////////////////////
  13:   
  14:      /// <summary publish="true">
  15:      /// ODBC support class.
  16:      /// </summary>
  17:      /// <remarks> 
  18:      /// Copyright © 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  19:      ///
  20:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  21:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  22:      ///
  23:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  24:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  25:      /// 
  26:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  27:      /// 
  28:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  29:      /// </remarks> 
  30:      public class Odbc
  31:      {
  32:          private string connectionString;
  33:   
  34:          ////////////////////////////////////////////////////////////////////////////
  35:   
  36:          /// <summary>
  37:          ///
  38:          /// </summary>
  39:          public Odbc()
  40:          {
  41:              connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
  42:          }
  43:   
  44:          ////////////////////////////////////////////////////////////////////////////
  45:   
  46:          /// <summary>
  47:          ///
  48:          /// </summary>
  49:          public Odbc(string _connectionString)
  50:          {
  51:              connectionString = _connectionString;
  52:          }
  53:   
  54:          ////////////////////////////////////////////////////////////////////////////
  55:   
  56:          /// <summary>
  57:          ///
  58:          /// </summary>
  59:          public bool Sql(string sql)
  60:          {
  61:              // execute an SQL command
  62:              bool b = true;
  63:              OdbcCommand sco;
  64:   
  65:              using (OdbcConnection sc = new OdbcConnection(connectionString))
  66:              {
  67:                  sco = new OdbcCommand();
  68:   
  69:                  sco.CommandType = CommandType.Text; // default
  70:                  sco.CommandText = sql;
  71:                  sco.Connection = sc;
  72:   
  73:                  sc.Open();
  74:                  sco.ExecuteNonQuery();
  75:   
  76:                  //sc.Close();
  77:              }
  78:   
  79:              return b;
  80:          }
  81:   
  82:          ////////////////////////////////////////////////////////////////////////////
  83:   
  84:          /// <summary>
  85:          ///
  86:          /// </summary>
  87:          public DataTable Select(string sql)
  88:          {
  89:              // return a DataTable of result rows
  90:              OdbcCommand sco;
  91:   
  92:              DataSet ds = new DataSet();
  93:              DataTable dt = new DataTable();
  94:              OdbcDataAdapter da = new OdbcDataAdapter();
  95:   
  96:              try
  97:              {
  98:                  using (OdbcConnection sc = new OdbcConnection(connectionString))
  99:                  {
 100:                      sco = new OdbcCommand(sql, sc);
 101:   
 102:                      sc.Open();
 103:   
 104:                      da.SelectCommand = sco;
 105:   
 106:                      da.Fill(ds);
 107:   
 108:                      //sc.Close();
 109:                  }
 110:   
 111:                  dt = ds.Tables[0];
 112:              }
 113:              catch (Exception)
 114:              {
 115:                  dt = null;
 116:              }
 117:   
 118:              return dt;
 119:          }
 120:   
 121:          ////////////////////////////////////////////////////////////////////////////
 122:   
 123:          /// <summary>
 124:          ///
 125:          /// </summary>
 126:          public string Scalar(string sql)
 127:          {
 128:              // return a scaler
 129:              string s;
 130:   
 131:              OdbcCommand sco;
 132:   
 133:              using (OdbcConnection sc = new OdbcConnection(connectionString))
 134:              {
 135:                  sco = new OdbcCommand(sql, sc);
 136:   
 137:                  sc.Open();
 138:   
 139:                  try { s = sco.ExecuteScalar().ToString(); }
 140:                  catch { s = null; }
 141:   
 142:                  sc.Close();
 143:              }
 144:   
 145:              return s;
 146:          }
 147:   
 148:          ////////////////////////////////////////////////////////////////////////////
 149:   
 150:          /// <summary>
 151:          ///
 152:          /// </summary>
 153:          public string SmallDateTime(DateTime dt)
 154:          {
 155:              // return an SQL friendly string of a smalldatetime value
 156:              string s;
 157:   
 158:              s = dt.ToString("yyyy-MM-ddTHH:mm:ss");
 159:   
 160:              return s;
 161:          }
 162:   
 163:          ////////////////////////////////////////////////////////////////////////////
 164:          ////////////////////////////////////////////////////////////////////////////
 165:      }
 166:  }