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.