I'm trying to read all the tables of an MS-SQL database into a DataSet, which consists of a group of DataTables. For that, I have written this piece of source code:
try
{
DbConnection.Open();
sqlCommand = DbConnection.CreateCommand();
sqlCommand.CommandText = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES";
// INFORMATION_SCHEMA.TABLES contains the names of all the tables.
sqlDataReader = sqlCommand.ExecuteReader();
cmb_Table_Names.Items.Clear(); // combobox for keeping table names.
while (sqlDataReader.Read())
{
string tmp_Table_Name = sqlDataReader.GetString(0);
cmb_Table_Names.Items.Add(tmp_Table_Name);
DataTable dt_tmp = new DataTable();
using (var da = new SqlDataAdapter($"SELECT * FROM {tmp_Table_Name}",
DbConnection))
{
da.Fill(dt_tmp); // see "stackoverflow.com/questions/68919147"
}
dataSet.Tables.Add(dt_tmp);
}
sqlDataReader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Reading DB failed!!!", MessageBoxButton.OK);
}
I'm falling into my Exception
at line da.Fill(dt_tmp);
.
The result of ? ex
in the immediate window is:
{"There is already an open DataReader associated with this Command which must be closed first."}
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233079
HelpLink: null
InnerException: null
Message: "There is already an open DataReader associated with this Command which must be closed first."
Source: "System.Data"
StackTrace: " at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)\r\n
at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)\r\n
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)\r\n
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)\r\n
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)\r\n
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)\r\n
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)\r\n
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)\r\n
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)\r\n
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)\r\n
at Database_Handling.MainWindow.Btn_Read_DB_Click(Object sender, RoutedEventArgs e) in <filename>
TargetSite: {Void ValidateConnectionForExecute(System.Data.SqlClient.SqlCommand)}
What does this mean (especially since I only have one SqlDataReader
)?
Does it mean that I need to close the SqlAdapter
da
or something? (I already checked: there is no da.Close()
method)
Does it mean that I can't launch an SQL command while another is still being read? (This would be a major limitation, I guess)
...
Does anybody have an idea?
Thanks in advance