Encapsulating SqlDataReader and SQL

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;
}
}
}
}
 
J

John Beyer

Adie said:
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 :)

Don't bother re-inventing the wheel...get the Microsoft Data Access Block as
it does exactly what you are attempting to do... most everything data-access
related you will want to do can be done in 1 line of code.

http://www.microsoft.com/downloads/...0a-9877-4a7b-88ec-0426b48df275&displaylang=en

HTH...

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top