home
links
tips

code
users
tools
tutorials
projects
web
help
design

mudabone
suletzki
trans48
nutrition reality

MarthaAttire!

code Data Base OLEDB


using System;
using System.Data;
using System.Data.OleDb;

namespace dbOLEDB
{
     /// <summary>
     /// OleDB database class.  Manages connections, does basic DB work.
     /// </summary>
     public class database
     {
          private string sConn;
          private System.Data.OleDb.OleDbConnection conn;

          /// <summary>
          /// Constructor: requires db connection string
          /// </summary>
          /// <param name="DatabaseConnection">a db connection string, example: sConn = @"driver={SQL Server};server=BWISELEYBWISELEY2K;uid=sa;pwd=;database=miniSpecs</param>
          public database(string DatabaseConnection)
          {
               this.DBConnection = DatabaseConnection;
          }

          public DataSet getDataSet(string sql)
          {
               System.Data.OleDb.OleDbDataAdapter da = new OleDbDataAdapter();
               da.SelectCommand = new OleDbCommand(sql,conn);
               System.Data.DataSet ds = new System.Data.DataSet();
               da.Fill(ds);
               return ds;
          }

          public void execSQL(string sql)
          {
               try
               {
                    System.Data.OleDb.OleDbCommand cmd = conn.CreateCommand();
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
               }
               catch (System.Data.OleDb.OleDbException e)
               {
                    debugPrint(e.Message);
               }
               finally {}
          }

          public string getFirstRowFirstValue(string sql)
          {
               string sRes = "";

               try
               {
                    System.Data.DataSet ds = this.getDataSet(sql);
                    sRes = ds.Tables[0].Rows[0][0].ToString();
                    ds.Dispose();
               }
               catch {}

               return sRes;
          }

          public bool tableExists(string sTableName)
          {
               string sql = "delete from " + sTableName + " where 1=0";
               try
               {
                    System.Data.OleDb.OleDbCommand cmd = conn.CreateCommand();
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                    return true;
               }
               catch (System.Data.OleDb.OleDbException e)
               {
                    debugPrint(e.Message);
                    return false;
               }
               
          }

          public bool rowExists(string sTableName, string sRowName, string sRowValue)
          {
               string sql = "select count(*) cc from " + sTableName + " where " + sRowName + " = '" + sRowValue + "'";
               System.Data.DataSet ds = this.getDataSet(sql);
               if ((int)ds.Tables[0].Rows[0]["cc"] > 0)
                    return true;
               else
                    return false;
          }

          private void debugPrint(string s)
          {
               System.Diagnostics.Debug.WriteLine(s);
          }

          #region properties
          public string DBConnection
          {
               set 
               { 
                    this.sConn = value;
                    conn = new OleDbConnection(value);
                    conn.Open();
               }
               get { return this.sConn; }
          }
          #endregion
     }
}


Last Modified 10/2/03 1:17 PM

Hide Tools