473,394 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Encapsulating SqlDataReader and SQL

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("ConnectionS tring"));
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("ConnectionS tring"));

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

Nov 15 '05 #1
2 1819
"Adie" <ar*******@h-o-t-m-a-i-l.com> wrote in message
news:9d******************************@news.teranew s.com...
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/d...displaylang=en

HTH...

John
Nov 15 '05 #2
John Beyer wrote:
"Adie" <ar*******@h-o-t-m-a-i-l.com> wrote in message
news:9d******************************@news.terane ws.com...
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/d...displaylang=en


Oh good, someones done it already, just what I was hoping for!

Thanks.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ricola ! | last post by:
Why do I say: SqlDataReader dr; instead of SqlDataReader dr = new SqlDataReader();
7
by: Franck Diastein | last post by:
Hi, when I call ExportData I have this error: Invalid attempt to Read when reader is closed. Telling me that there's a problem with this line: while(_dataR.Read()){ Code:...
3
by: Neil Guyette | last post by:
Hello, Everyone, I'm trying to find information on how to populate a combo box using a SqlDataReader. I want to be able to set the value of the combo's value property different then the...
1
by: Arvind P Rangan | last post by:
Hi All, How do you get all the values of a sqldatareader if it contains multiple resultset. Using sqldatareader.nextresult and sqldatareader.read e.g. While sqldatareader.read ' If not...
4
by: Michael Carr | last post by:
I have a function that populates a class with values from a database. I'd like to pass into the function either a SqlDataReader or a DataRow, depending on which mechanism I'm using to retrieve data...
1
by: me | last post by:
Why is SqlDataReader not present in visual studios Toolbox->Data where I find components like DataSet? Most examples in books and in vs.net are based on datasets but using disconnected tables seems...
8
by: bidllc | last post by:
I have a funtion that works fine and dandy when called from anywhere in my app. It will NOT work when called from inside the class in which it resides. This is the function I'm calling:...
4
by: mimi | last post by:
Hi Please help me out, I can't find a way to close a sqldatareader when error occur at statement cmd.ExecuteReader(). I can't close it in catch because it is local in try scope and I can't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.