473,404 Members | 2,213 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,404 software developers and data experts.

Seperate Database and Data

G'day,

I've separated my database calls into a separate dll, and return
results from any database calls as a SqlDataReader (because of SQL
Server use, it's nice and fast). However I'd like to be database
independent by replacing the dll with whatever database is being
supported. This would mean instead of returning SqlDataReaders another
way to return results would have to be used.

Is the best method to return database neutral ArrayLists of objects
(this would mean you'd have to cycle through the results twice, one to
add to the arraylist from the database results, and once more to
retrieve the objects from the arraylist) Or to use something like a
DataSet (which isn't as straightforward as a SqlDataReader)?

Have looked around Google for a bit but didn't find info that was
totally relevant.

Thanks,
-Mark

Nov 17 '05 #1
9 1491
> Is the best method to return database neutral ArrayLists of objects
(this would mean you'd have to cycle through the results twice, one to
add to the arraylist from the database results, and once more to
retrieve the objects from the arraylist) Or to use something like a
DataSet (which isn't as straightforward as a SqlDataReader)?

Just an idea: would it make sense to use System.Data.Odbc or
System.Data.OleDb? These are compatible with most major databases.

Greetings,
Wessel
Nov 17 '05 #2

I'm not sure how you can have a database separated code relying on a ready
which requires an open database connection.

If you want to move the database related code into another library then the
library needs to take care of opening the connection, utilising the reader,
and closing the connection again.

This then allows you to change databases, as you've said, or even change it
so that any data input library could "feed" your core code. For example, you
should be able to have a text file reader class which gives you back the
data in the required format, and switching between SQL Server, Oracle, a
text file, message queue, Xml etc etc should be down to configuration after
the application is compiled.

In terms of what the data should look like, this depends on the criteria for
the core code, and then it is up to each "plug in" to normalize the data
into what it knows the core is expecting.
If the core is expecting a single value, for example, this is different to
whether it deals with an Xml structure, data table, and so on.

If you provide more details on what the data is, and what the application is
doing with it, perhaps we can suggest a few ways that may help you on your
way.

Dan.
<sh**********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
G'day,

I've separated my database calls into a separate dll, and return
results from any database calls as a SqlDataReader (because of SQL
Server use, it's nice and fast). However I'd like to be database
independent by replacing the dll with whatever database is being
supported. This would mean instead of returning SqlDataReaders another
way to return results would have to be used.

Is the best method to return database neutral ArrayLists of objects
(this would mean you'd have to cycle through the results twice, one to
add to the arraylist from the database results, and once more to
retrieve the objects from the arraylist) Or to use something like a
DataSet (which isn't as straightforward as a SqlDataReader)?

Have looked around Google for a bit but didn't find info that was
totally relevant.

Thanks,
-Mark

Nov 17 '05 #3
True, that'd be cool but then you wouldn't get to use the nice sql
server stuff. Using oledb and Datasets might be the way to go.

Nov 17 '05 #4
e.g

DatabaseDll n = new DatabaseDll();
SqlDataReader t = n.SomeMethodThatReturnsReader();

while(t.Read())
{
Something.Add(t["DataField1"].Tostring());
}
t.Close();

Would be the basic idea, the read is just a quicker means to pass the
data.

If an arraylist was used, you'd get something like

DatabaseDll n = new DatabaseDll();
ArrayList t = n.SomeMethodThatReturnsReader();

ForEach(object e in t)
{
Something.Add(e.NameMe.ToString());
}

If you get my reasoning, it's just a means of feeding the data back.
Obviously an SqlReady isn't the best idea, but the Arraylist would mean
two cycles. DataSet the answer? The arraylist would allow a plug-able
style of programing i think?
thanks for the ideas guys,
-mark

Nov 17 '05 #5
Mark,

In your situation I'd go for an 'interfaced' approach. Instead of returning
SqlDataReader objects from your methods, they could return IDataReader (the
interface implemented by SqlDataReader, amongst others)compatible objects.
This could be a SqlDataReader, but also MySqlDataReader or an implementation
of this interface for any other database (Oracle, DB2, etc.) dependend on
the database you're using.

IDbConnection conn = null;
string connStr = "my connection string";
switch (connType)
{
case SQL:
conn = new SqlConnection(connStr);
break;
case MySQL:
conn = new MySqlConnection(connStr);
break;
}

if(connType == Connection.SQL)
conn = new SqlConnection(connStr);

IDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM TABLE";
IDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
}

Hope this helps,
Raymond

<sh**********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
G'day,

I've separated my database calls into a separate dll, and return
results from any database calls as a SqlDataReader (because of SQL
Server use, it's nice and fast). However I'd like to be database
independent by replacing the dll with whatever database is being
supported. This would mean instead of returning SqlDataReaders another
way to return results would have to be used.

Is the best method to return database neutral ArrayLists of objects
(this would mean you'd have to cycle through the results twice, one to
add to the arraylist from the database results, and once more to
retrieve the objects from the arraylist) Or to use something like a
DataSet (which isn't as straightforward as a SqlDataReader)?

Have looked around Google for a bit but didn't find info that was
totally relevant.

Thanks,
-Mark

Nov 17 '05 #6
Ah yes this is more what i was thinking of, Thanks :D

Nov 17 '05 #7
DatabaseDll n = new DatabaseDll();
SqlDataReader t = n.SomeMethodThatReturnsReader();

while(t.Read())
{
Something.Add(t["DataField1"].Tostring());
}
t.Close();

This wouldn't work because you don't have a database connection open...

If an arraylist was used, you'd get something like

DatabaseDll n = new DatabaseDll();
ArrayList t = n.SomeMethodThatReturnsReader();

ForEach(object e in t)
{
Something.Add(e.NameMe.ToString());
}

If you get my reasoning, it's just a means of feeding the data back.
Obviously an SqlReady isn't the best idea, but the Arraylist would mean
two cycles. DataSet the answer? The arraylist would allow a plug-able
style of programing i think?
thanks for the ideas guys,
-mark


If you're going to make an extensible solution then the plug in would need
to normalize the data (I.E, parse it at least once) extracting the data and
pushing it into the required structure.
Any generic structure would do the trick. ArrayList, SortedList, your own
defined struct, etc...

Your plugin would have something like this:

// declare and initialise variables containing connection string
// stored procedure info etc up here...

public ArrayList RetrieveData()
{
ArrayList myData = new ArrayList();
SqlConnection dbConnection = new SqlConnection();

try
{
dbConnection.Open();

SqlCommand spCommand = new SqlCommand();
// initialise the command here

SqlDataReader dbReader = spCommand.ExecuteQuery();

while ( dbReader.Read() )
{
myData.Add ( dbReader[0].ToString() );
}

dbReader.Close();

}
catch ( Exception ex )
{
// do something with the error here, throw it again if necessary...
}
finally
{
// call even if an exception occurs
dbConnection.Close();
}
return myData;
}

Your core code would then simply call this method.

// initialise the object instance here...

ArrayList myData = myDatabaseObjectInstance.RetrieveData();

// do what you will with your Data...

To make this extensible, you'd inherit from an interface which ensures that
all derived classes have public RetrieveData() instantiated... Then an
oracle library would work by simply creating the same sort of class, using
the Oracle client and syntaxes instead.

Hope that helps.
Nov 17 '05 #8
Why catch a steam train when you've a more modern electric one instead?

<sh**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
True, that'd be cool but then you wouldn't get to use the nice sql
server stuff. Using oledb and Datasets might be the way to go.

Nov 17 '05 #9
Er yeah man i already suggested this, my worry with doing it this way
is doing a cycle of the results twice, and the overhead that would
cause. I guess if you a "plugable" type struct that's the best to do
it.
Dan Bass wrote:
DatabaseDll n = new DatabaseDll();
SqlDataReader t = n.SomeMethodThatReturnsReader();

while(t.Read())
{
Something.Add(t["DataField1"].Tostring());
}
t.Close();

This wouldn't work because you don't have a database connection open...


This does work by setting the connection to stay open till the
sqldatareader is closed. Bad open connections? Probably but it works
nicely.

Thanks,
-Mark
If an arraylist was used, you'd get something like

DatabaseDll n = new DatabaseDll();
ArrayList t = n.SomeMethodThatReturnsReader();

ForEach(object e in t)
{
Something.Add(e.NameMe.ToString());
}

If you get my reasoning, it's just a means of feeding the data back.
Obviously an SqlReady isn't the best idea, but the Arraylist would mean
two cycles. DataSet the answer? The arraylist would allow a plug-able
style of programing i think?
thanks for the ideas guys,
-mark


If you're going to make an extensible solution then the plug in would need
to normalize the data (I.E, parse it at least once) extracting the data and
pushing it into the required structure.
Any generic structure would do the trick. ArrayList, SortedList, your own
defined struct, etc...

Your plugin would have something like this:

// declare and initialise variables containing connection string
// stored procedure info etc up here...

public ArrayList RetrieveData()
{
ArrayList myData = new ArrayList();
SqlConnection dbConnection = new SqlConnection();

try
{
dbConnection.Open();

SqlCommand spCommand = new SqlCommand();
// initialise the command here

SqlDataReader dbReader = spCommand.ExecuteQuery();

while ( dbReader.Read() )
{
myData.Add ( dbReader[0].ToString() );
}

dbReader.Close();

}
catch ( Exception ex )
{
// do something with the error here, throw it again if necessary...
}
finally
{
// call even if an exception occurs
dbConnection.Close();
}
return myData;
}

Your core code would then simply call this method.

// initialise the object instance here...

ArrayList myData = myDatabaseObjectInstance.RetrieveData();

// do what you will with your Data...

To make this extensible, you'd inherit from an interface which ensures that
all derived classes have public RetrieveData() instantiated... Then an
oracle library would work by simply creating the same sort of class, using
the Oracle client and syntaxes instead.

Hope that helps.


Nov 17 '05 #10

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

Similar topics

5
by: TonyB | last post by:
Hi, I've searched the group and need more information and guidance on this issue I need to resolve next week. I work for the local school system and I am working on a way to parse a CSV file of...
6
by: jason | last post by:
Products ---> Products|Customer <--Customer With regardst the above many-to-many relationship: Is there any advantage in creating seperate access databases for: - Customer - Products ...
0
by: KB | last post by:
I am creatig a database in 2000 where I need to be able to have the users add data and information into lookup tables. That is no problem, I have a form for that, that works fine. This...
0
by: Steven Smith | last post by:
Hi guy's I've hit a bit of a sticking point, I'm sure this is simple enough but I just can't get my head around it and I'm looking for a bit of direction. When dealing with FileIO up until now I...
17
by: Scot L. Harris | last post by:
I am running postgresql 7.2.4 on a Redhat 8.0 system. I have been looking for a way to setup another database besides the initial one setup under /var/lib/pgsql/data on a different file system. ...
6
by: Phil Latio | last post by:
I am missing a trick here on how use variables defined in seperate file. In script 1, I have proved to myself my class is working OK because I can pass in the values and the echo statement...
6
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
0
by: B.N.Prabhu | last post by:
Hi, I have a DataTable with several rows. Its having 20 Columns. when i click the Insert button then i have to check the Database Rows. Whether these new rows are already available in the...
6
by: Relaxin | last post by:
I'm coming from Borland C++ and am "somewhat" new to C#, but very seasoned in C++. I'm looking for a specific feature that Borland had that I can't seem to find in C#. In Borland that feature...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.