I have a 'worker' function that will be processing any and all sql queries in my program. I will need to execute queries that return result sets and ones that just execute stored procedures without any results. Is this possible with MySqlDataAdapter.Fill or do I need to use 开发者_开发问答the MySqlCommand.ExecuteNonQuery() method? Here is my 'worker' function for reference:
private DataSet RunQuery(string SQL)
{
MySqlConnection connection;
MySqlCommand command;
MySqlDataAdapter adapter;
DataSet dataset = new DataSet();
lock(locker)
{
connection = new MySqlConnection(MyConString);
command = new MySqlCommand();
command = connection.CreateCommand();
command.CommandText = SQL;
adapter = new MySqlDataAdapter(command);
adapter.Fill(dataset);
}
return dataset;
}
Firstly your worker function would throw an exception on the .Fill(dataset) method. You need to construct your adapter with the select command as:
adapter = new MySqlDataAdapter(command);
The result of a nonquery running against a Fill will result in a dataset with no tables returned.
However: Using ADO.NET commands like Adapter.Fill(dataset) to do a non-query command is very inefficient when compared to using the cmd.ExecuteNonQuery() method.
Besides the construction of a separate and redundant DataAdapter object, every call being made in ADO.NET is virtual and needs to be resolved to a call site at run time by the CLR. Under load, running this method continuously will result in a lot of GC pressure with the DataAdapter object needing continuous disposal, and will be noticibly slower than just running command.ExecuteNonQuery();
精彩评论