At the moment, I have the following to do MySQL queries:
MySqlCommand Command = new MySqlCommand(Query, SQLConnection);
IAsyncResult Result = Command.BeginExecuteReader();
return Command.EndExecuteReader(Result);
This is a static command in a server in the database class, which includes a static connect/disconnect function (used once when the server starts and stops respectively) and is accessed by every client. This however is clearly inefficient as when multiple clients are on I often get the error "There is already an open datareader on this connection" (I have try/catch blocks around the code but removed them in the post to simplify the code.
Now, I believe there are two ways in which I can fix this error and make the mysql queries/updates much more efficient:
1 - Remove the static from the class, and make every user connection and such have their own mysql connection class. The problem I see with this is that having a lot of classes connection to the database at once would take up a lot of memory, and since some classes are static (like the main server class) they'll still be accessed by multiple clients at once on the same connection thus causing the error.
2 - Keep the class static but don't connect/disconnect in the start/stop methods of the server, instead use the using() function to connect to the SQL database every time a query is needed and then fill a datareader with the results (which is returned to the client class for processing). I'm not sure how efficient constantly connecting/disconnecting to the database would be though.
Basically, I need to be able to (in theory, not all the time but often) be able to execute multiple queries at the same time (roughly, usually within several milliseconds) and return the datasets. Due to this being a server with many clients that expect their data instantly this needs to be fast.
So tl;dr would method 2 (connect to the database seperately for each query and fill a datatable which is returned) be efficient? If not, what else could I do?