Use of undocumented sp_MSforeachtable and MStablespace

H

Hans-Peter

What is wrong with the following code? What I want to do is to store
the results of MStablespace for every table in a DataTable. The result
contains the number of rows and the data space and the index space.
This seems to be okay, because dataTable has three columns after
executing the code. The problem is, that I get only one row.

using ( SqlConnection conn = new SqlConnection(connString)
{
conn.Open();
SqlCommand cmd = new SqlCommand("sp_MSforeachtable", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@command1","exec sp_MStablespace '?'");
SqlDataAdapter adapter = new SqlDataAdapter( cmd );
DataTable dataTable = new DataTable("Statistics");
adapter.Fill(dataTable);
}

Thanks for any hints,
Hans
 
D

David Browne

Hans-Peter said:
What is wrong with the following code? What I want to do is to store
the results of MStablespace for every table in a DataTable. The result
contains the number of rows and the data space and the index space.
This seems to be okay, because dataTable has three columns after
executing the code. The problem is, that I get only one row.

using ( SqlConnection conn = new SqlConnection(connString)
{
conn.Open();
SqlCommand cmd = new SqlCommand("sp_MSforeachtable", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@command1","exec sp_MStablespace '?'");
SqlDataAdapter adapter = new SqlDataAdapter( cmd );
DataTable dataTable = new DataTable("Statistics");
adapter.Fill(dataTable);
}

That command will return N different 1-row result sets. The DataAdapter
will only fill the DataTable with the first result set.

Something like:

using (SqlDataReader r = cmd.ExecuteReader())
{
while (true)
{
while (r.Read() )
{
//manually fill datatable
}
if (!r.NextResult() )
{
break;
}
}
}

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top