)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Odbc (Ia.Cl.Models.Db)

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