c#: how to get data from mySQL?

Hi all.
currently I have the following code:
static public DataTable getSQLresult(string inQuery) {
HelperDebug.addDebug("[start]:HelperDBMySQL.getSQLresult(string inQuery)",1);
string MyConString = "";
MyConString = "Provider=mySQLODBC; DRIVER={MySQL ODBC 3.51 Driver};" +
                                                                        "SERVER=localhost;" +
DataTable returnTable = new DataTable();
try {
      OdbcConnection myConnection = new OdbcConnection ();
      myConnection.ConnectionString = MyConString;
      OdbcDataAdapter adp11 = new OdbcDataAdapter(inQuery, myConnection);
      try {
      } catch (Exception myError) {
} catch (Exception myError) {
return returnTable;

1) How do I put data form a select query to the "returnTable"?
2) Is this a senseful method?
2a) Would you do it like this or is there a general mistake?
2b) Are the positions for opening and closing myConnection ate a good point?
2c) Should I return DataGrids? How?

Thank you
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SmoerbleAuthor Commented:
No answers? Why?
you could fill a dataset with the dataadapter and return that....

Dataset ds = new Dataset();

adp11.Fill(ds, "A_Table_Name");

return ds;

I would initialise everything outside the try catch and only do the fill inside the try catch.
catch(Exception ex)

You dont need  connection.Open(); or connection.Close(); filling a dataset with an adapter.


SmoerbleAuthor Commented:
Thank you for the answer.
If I fill it with an adapter, can I still do queries that contain JOINS etc? Or does this make things more complicated?
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.


you can do whatever query you like, containing joins, declares etc.  You may want to look into stored procedures if they get silly big but there is nothing stopping you having a huge query in a string and simple giving it to the adapter in the same way.

SmoerbleAuthor Commented:
I would prefere to use stored procs. But right now I think I can not understand how to create danymicaly generated queries when using stored procs. So i have to got with normal queries (which might cause issues with sql injection).
Sometimes it is difficult to use stored procs when for example the query is dependant on several drop down lists.  You are better off building the query in your code.
You should not suffer problems talking to the SQL server unless the queries are massive i guess.

To build your queries i would use a StringBuilder.  look at the bottom section of this page...
you can then build your string through a stage of if statements (or whatever) to build the correct clause into your query.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.