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_AddNewProdu ct : System.Windows. Forms.Form
private void frm_AddNewProdu ct_Load(object sender, System.EventArg s e)
{ ...
DataAccess DataAccess = new DataAccess();
DS_Products = DataAccess.GetP roducts();
.... }
----------------------------
public class DataAccess
{ ....
public DataSet GetProducts()
{
string mySql = "Select itemid, Description, Quantity, Price from
products where producttype = 'P'";
DBConnection myConn = new DBConnection("S QLServer", mySql); //the
SQLServer parameter is not being used now
DataSet DS_MyProducts = new DataSet();
DS_MyProducts = myConn.Connecti on();
return DS_MyProducts;
}
}
----------------
public class DBConnection
{
public DataSet MyClientsDS;
public SqlDataAdapter MyDataAdapterCl ients;
string strThisConn, strThisQuery, strTable;
public DBConnection(st ring strDataBase, string strQuery)
{
strThisConn = strDataBase;
strThisQuery = strQuery;
strTable = strMainTable;
}
public DataSet Connection()
{
MyClientsDS = new DataSet();
SqlConnection SQLConn = new SqlConnection(" Data Source=localhos t;
Integrated Security=SSPI;" +
"Initial Catalog=invoici ng");
MyDataAdapterCl ients = new SqlDataAdapter (strThisQuery, SQLConn);
MyDataAdapterCl ients.Fill(MyCl ientsDS);
SQLConn.Close() ;
return MyClientsDS;
}
}
Thanks for your help.