I am at the point where I feel that I am not doing it right. It works and does the job, but I am sure that there are more efficient and smarter ways of doing it.
I would like to see if there is a way of making it more efficient, clear, and easier to use.
This is my query getter:
public DataTable querySQL_DT_Return(String query)
{
var form = Form.ActiveForm as Form1;
DataTable RETURNME = null;
OleDbConnection conn = new OleDbConnection(cnStr);
try
{
try
{
conn.Open();
}
catch
{
MessageBox.Show("Can't connect to the database!");
}
OleDbDataAdapter Data1 = null;
try
{
Data1 = new OleDbDataAdapter(query, conn);
}
catch
{
MessageBox.Show("Can't open the connection to the DB, please try again!");
return RETURNME;
}
DataSet a = new DataSet();
Data1.Fill(a);
DataTable dt = a.Tables[0];
//Adding data to the columns
RETURNME = dt;
}
catch
{
//MessageBox.Show("Having issues retrieving data from Database, please try again!");
// return RETURNME;
}
finally
{
if (conn.State.ToString() == "Open")
{
conn.Close();
}
}
if (conn.State.ToString() == "Open")
{
conn.Close();
}
return RETURNME;
}
When I receive the DataTable, I just loop through it getting records.
IList<CustomObject>
where you define theCustomObject
class. I have helper methodsRunQueryExpectOneTable
andRunProcExpectOneTable
which both return aDataTable
. Then, you can call these and iterate over the rows ... \$\endgroup\$