home
links
tips

code
users
tools
tutorials
projects
web
help
design

mudabone
suletzki
trans48
nutrition reality

MarthaAttire!

code Database SQL


using System;
using System.Data;
using System.Data.SqlClient;

namespace dbSQL
{
     /// <summary>
     /// Summary description for databaseSQL.
     /// </summary>
     public class databaseSQL
     {
          private System.Data.SqlClient.SqlConnection conn;

          public databaseSQL(string SQLConnectionString)
          {
               conn = new SqlConnection(SQLConnectionString);
               conn.Open();
          }

          public databaseSQL(string Server, string Database, string UserID, string Password)
          {
               //Server=BWISELEYBWISELEY2K;Database=miniSpecs;User ID=sa;
               conn = new SqlConnection("Server=" + Server + ";Database=" + Database + ";User ID=" + UserID + "; Password=" + Password);
               conn.Open();
          }

          public DataSet getDataSet(string sql)
          {
               System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter();
               da.SelectCommand = new SqlCommand(sql,conn);
               System.Data.DataSet ds = new System.Data.DataSet();
               da.Fill(ds);
               return ds;
          }

          public void execSQL(string sql)
          {
               try
               {
                    System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
               }
               catch (System.Data.SqlClient.SqlException 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.SqlClient.SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                    return true;
               }
               catch (System.Data.SqlClient.SqlException 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);
          }
     }
}


Comments:

From Darren Telling [86.131.1.237] - 9/20/05 8:16 AM

For retreiving a single field value from the first row, it is much more efficient to use ExecuteScalar()



Last Modified 10/2/03 1:18 PM

Hide Tools