473,387 Members | 1,864 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,387 software developers and data experts.

OOP question and correct class usage for connecting to DB

Jim
I'm writing an Invoicing Windows app but I'm writing it to make the code as
easy to maintain as possible. Basically, to get any records from my DB, I
use two classes: one that sets up the SQL statement, and another that makes
the connection (plus the class that contains the windows form). Now, is
this the best way to divide my tasks? And by using this structure, how would
I be able to update my dataset when I don't have direct access to the
dataadapter? Any suggestions on how this would be better organized would be
really appreciated.
This is how the code looks like:
--------------------
public class frm_AddNewProduct : System.Windows.Forms.Form

private void frm_AddNewProduct_Load(object sender, System.EventArgs e)

{ ...

DataAccess DataAccess = new DataAccess();

DS_Products = DataAccess.GetProducts();

.... }

----------------------------

public class DataAccess

{ ....

public DataSet GetProducts()
{
string mySql = "Select itemid, Description, Quantity, Price from
products where producttype = 'P'";
DBConnection myConn = new DBConnection("SQLServer", mySql); //the
SQLServer parameter is not being used now
DataSet DS_MyProducts = new DataSet();
DS_MyProducts = myConn.Connection();
return DS_MyProducts;
}
}
----------------
public class DBConnection
{
public DataSet MyClientsDS;
public SqlDataAdapter MyDataAdapterClients;
string strThisConn, strThisQuery, strTable;
public DBConnection(string strDataBase, string strQuery)
{
strThisConn = strDataBase;
strThisQuery = strQuery;
strTable = strMainTable;
}
public DataSet Connection()
{
MyClientsDS = new DataSet();
SqlConnection SQLConn = new SqlConnection("Data Source=localhost;
Integrated Security=SSPI;" +
"Initial Catalog=invoicing");
MyDataAdapterClients = new SqlDataAdapter (strThisQuery, SQLConn);
MyDataAdapterClients.Fill(MyClientsDS);
SQLConn.Close();
return MyClientsDS;
}
}

Thanks for your help.
Nov 15 '05 #1
2 1530
Hi Jim

Firstly, as you seem to be using SQLServer, I'd really recommend that you
use stored procedures.
If for only one reason, they will give you better maintainability.

I've taken the following approach in a Win32 app that proved to be quite
effective, especially in terms of reusability and maintainability:
// hide all data access/business logic inside static classes that represent
business entities
DataTable dt= Student.FetchByFirstLast(txtFirstName.Text,txtLast Name.Text);
if(dt.Rows.Count>0)
{
// .......
Inside this "Student" class I call a data access class (very similar to the
DAAB from MS).
Using this approach, with an atomic class, I can concentrate on the business
logic.

For updates, I pass the individual params to an "Update" on the respective
classes.
HTH

Cheers,

Simon Stewart
Johannesburg, South Africa
"Jim" <NA> wrote in message news:ec*************@TK2MSFTNGP10.phx.gbl...
I'm writing an Invoicing Windows app but I'm writing it to make the code as easy to maintain as possible. Basically, to get any records from my DB, I
use two classes: one that sets up the SQL statement, and another that makes the connection (plus the class that contains the windows form). Now, is
this the best way to divide my tasks? And by using this structure, how would I be able to update my dataset when I don't have direct access to the
dataadapter? Any suggestions on how this would be better organized would be really appreciated.
This is how the code looks like:
--------------------
public class frm_AddNewProduct : System.Windows.Forms.Form

private void frm_AddNewProduct_Load(object sender, System.EventArgs e)

{ ...

DataAccess DataAccess = new DataAccess();

DS_Products = DataAccess.GetProducts();

... }

----------------------------

public class DataAccess

{ ....

public DataSet GetProducts()
{
string mySql = "Select itemid, Description, Quantity, Price from
products where producttype = 'P'";
DBConnection myConn = new DBConnection("SQLServer", mySql); //the
SQLServer parameter is not being used now
DataSet DS_MyProducts = new DataSet();
DS_MyProducts = myConn.Connection();
return DS_MyProducts;
}
}
----------------
public class DBConnection
{
public DataSet MyClientsDS;
public SqlDataAdapter MyDataAdapterClients;
string strThisConn, strThisQuery, strTable;
public DBConnection(string strDataBase, string strQuery)
{
strThisConn = strDataBase;
strThisQuery = strQuery;
strTable = strMainTable;
}
public DataSet Connection()
{
MyClientsDS = new DataSet();
SqlConnection SQLConn = new SqlConnection("Data Source=localhost;
Integrated Security=SSPI;" +
"Initial Catalog=invoicing");
MyDataAdapterClients = new SqlDataAdapter (strThisQuery, SQLConn);
MyDataAdapterClients.Fill(MyClientsDS);
SQLConn.Close();
return MyClientsDS;
}
}

Thanks for your help.

Nov 15 '05 #2
Jim,
Its fine to seperate out your data access layer from the rest of your code
if you need or plan to have varying data sources (any exnterprise level
application would). What your doing here is a little off point though. A
data access layer tends to not only know how to connect to a database, but
know how to do the work as well (and know which tool in the shed to use).
Here, you are setting yourself up for one type of connection, a sql server
connection, so the only gain you have here is changing your database (not
really running multiple databases in parelles). Additionaly, your connection
method is always returning a DataSet. DataSets tend to be big and bulky and
should only be used when absolutly necessary. Most
of your application will likely benefit from a DataReader instead of a data
set (and btw, you don't have to close a connection if you didn't open it
outside of adapater). I am assuming from looking at this code, your client
classes know how to work with the data being returned in any way (be that a
data table, a data set, a reader, an xml flat file, whatever). So your not
getting much gain in terms of this class doing any real work. Pluss, your
creating a new object every time to want to make a call to your database
(this new class).
For maintainability, your on the right track. there isn't anything wrong
with wanting to have a data access layer that handles connections to various
database, the retriving of data and the modification of data. I would just
suggest you take a look at some design patterns that help you solve
particular problems in the data tier and build on that rather than having a
general knowledge of OOP and trying to solve problems that have been solved
many times over.
Wish i had a more specific answer for you, but without knowing more about
the app and what you already know regarding OOAD and patterns, this is the
best I can do.

-dec
"Jim" <NA> wrote in message news:ec*************@TK2MSFTNGP10.phx.gbl...
I'm writing an Invoicing Windows app but I'm writing it to make the code as easy to maintain as possible. Basically, to get any records from my DB, I
use two classes: one that sets up the SQL statement, and another that makes the connection (plus the class that contains the windows form). Now, is
this the best way to divide my tasks? And by using this structure, how would I be able to update my dataset when I don't have direct access to the
dataadapter? Any suggestions on how this would be better organized would be really appreciated.
This is how the code looks like:
--------------------
public class frm_AddNewProduct : System.Windows.Forms.Form

private void frm_AddNewProduct_Load(object sender, System.EventArgs e)

{ ...

DataAccess DataAccess = new DataAccess();

DS_Products = DataAccess.GetProducts();

... }

----------------------------

public class DataAccess

{ ....

public DataSet GetProducts()
{
string mySql = "Select itemid, Description, Quantity, Price from
products where producttype = 'P'";
DBConnection myConn = new DBConnection("SQLServer", mySql); //the
SQLServer parameter is not being used now
DataSet DS_MyProducts = new DataSet();
DS_MyProducts = myConn.Connection();
return DS_MyProducts;
}
}
----------------
public class DBConnection
{
public DataSet MyClientsDS;
public SqlDataAdapter MyDataAdapterClients;
string strThisConn, strThisQuery, strTable;
public DBConnection(string strDataBase, string strQuery)
{
strThisConn = strDataBase;
strThisQuery = strQuery;
strTable = strMainTable;
}
public DataSet Connection()
{
MyClientsDS = new DataSet();
SqlConnection SQLConn = new SqlConnection("Data Source=localhost;
Integrated Security=SSPI;" +
"Initial Catalog=invoicing");
MyDataAdapterClients = new SqlDataAdapter (strThisQuery, SQLConn);
MyDataAdapterClients.Fill(MyClientsDS);
SQLConn.Close();
return MyClientsDS;
}
}

Thanks for your help.

Nov 15 '05 #3

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

Similar topics

6
by: Peter Kleiweg | last post by:
I'm still new to Python. All my experience with OO programming is in a distant past with C++. Now I have written my first class in Python. The class behaves exactly as I want, but I would like to...
14
by: john.burton.email | last post by:
I've done some extensive searching and can't seem to find an answer to this - Is it correct to using "using" with templates, for example: using std::vector; Or do I need to specify the type...
53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
23
by: jm | last post by:
This works, but I don't know why. static void Main() { // Queue the task. ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc),new Form1()); Application.Run(); }
1
by: Tim T. | last post by:
I'm currently working on a report to forecast production for finished goods. The user can select one or more items to forecast. In addition, they may select one or more warehouses to view...
2
by: Reny | last post by:
Hi, I want to develop an internet usage monitor program in vb.net and monitor the time the user spend on internet etc. Can any one give me a clue on how to code it? Reny
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
4
by: Devon Null | last post by:
I have been exploring the concept of abstract classes and I was curious - If I do not define a base class as abstract, will it be instantiated (hope that is the right word) when a derived class is...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.