A
Adie
Hi, just looking for tips and ideas from the experienced.
I wondered if you guys encapsulate the SqlDataReader so as to allow
simpler code with less duplation, if so what does your code look like
and what methods do you provide?
My first effort is below, it's pretty sparce
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace repl
{
/// <summary>
// Class to return SqlDataReader with a sql string or
// update with sql string
/// </summary>
public class GenericData
{
public GenericData()
{
}
public SqlDataReader getByString(string _sql)
{
SqlConnection MySQLCon = new SqlConnection(
ConfigurationSettings.AppSettings.Get("ConnectionString"));
string sql = @_sql;
SqlCommand SqlCmd = MySQLCon.CreateCommand();
SqlCmd.CommandText = sql;
SqlDataReader SqlDr;
MySQLCon.Open();
SqlDr = SqlCmd.ExecuteReader();
return SqlDr;
}
public bool setByString(string _sql)
{
SqlConnection MySQLCon = new SqlConnection(
ConfigurationSettings.AppSettings.Get("ConnectionString"));
try
{
MySQLCon.Open();
string sql = @_sql;
SqlCommand SqlCmd = new SqlCommand();
SqlCmd.Connection = MySQLCon;
SqlCmd.CommandText = sql;
SqlCmd.CommandType = CommandType.Text;
SqlCmd.ExecuteNonQuery( );
return true;
}
catch (Exception e)
{
return false;
}
}
}
}
I wondered if you guys encapsulate the SqlDataReader so as to allow
simpler code with less duplation, if so what does your code look like
and what methods do you provide?
My first effort is below, it's pretty sparce

using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace repl
{
/// <summary>
// Class to return SqlDataReader with a sql string or
// update with sql string
/// </summary>
public class GenericData
{
public GenericData()
{
}
public SqlDataReader getByString(string _sql)
{
SqlConnection MySQLCon = new SqlConnection(
ConfigurationSettings.AppSettings.Get("ConnectionString"));
string sql = @_sql;
SqlCommand SqlCmd = MySQLCon.CreateCommand();
SqlCmd.CommandText = sql;
SqlDataReader SqlDr;
MySQLCon.Open();
SqlDr = SqlCmd.ExecuteReader();
return SqlDr;
}
public bool setByString(string _sql)
{
SqlConnection MySQLCon = new SqlConnection(
ConfigurationSettings.AppSettings.Get("ConnectionString"));
try
{
MySQLCon.Open();
string sql = @_sql;
SqlCommand SqlCmd = new SqlCommand();
SqlCmd.Connection = MySQLCon;
SqlCmd.CommandText = sql;
SqlCmd.CommandType = CommandType.Text;
SqlCmd.ExecuteNonQuery( );
return true;
}
catch (Exception e)
{
return false;
}
}
}
}