In a C # script, I try to read a table with an SqlDataReader object, and then drop the table. It's really that simple.
This is the code I'm using -
SqlConnection conn = getAWorkingDbConnection();
SqlCommand sqlCmd = new SqlCommand();
SqlDataReader dataReader;
sqlCmd.CommandTimeout = 0;
sqlCmd.Connection = conn;
sqlCmd.CommandText = "SELECT * FROM GlassTable";
dataReader = sqlCmd.ExecuteReader();
sqlCmd.CommandText = "DROP TABLE GlassTable";
sqlCmd.ExecuteReader();
I get this error - System.InvalidOperationException: there already exists an associated DataReader open with this command, which should be closed first.
I saw the API for the ExecuteReader method, but it does not answer my problem. Why does this error occur and how to fix it?
Thank.
Steam source
share