You are on page 1of 1

public void GetDbSize()

{
int sum = 0;

// Database Connection String


string sConnectionString = "Server = .; Integrated Security = true; database =
HKS";

// SQL Command [Same command discussed in section-B of this article]


string sSqlquery = "EXEC sp_MSforeachtable @command1=\"EXEC sp_spaceused '?'\" ";

DataSet oDataSet = new DataSet();

// Executing SQL Command using ADO.Net


using (SqlConnection oConn = new SqlConnection(sConnectionString))
{
oConn.Open();
using (SqlCommand oCmdGetData = new SqlCommand(sSqlquery, oConn))
{
oCmdGetData.ExecuteNonQuery();
SqlDataAdapter executeAdapter = new SqlDataAdapter(oCmdGetData);
executeAdapter.Fill(oDataSet);
}
oConn.Close();
}
// Iterating each table
for (int i = 0; i < oDataSet.Tables.Count; i++)
{
// We want to add only "data" column value of each table
sum = sum + Convert.ToInt32(oDataSet.Tables[i].Rows[0]
["data"].ToString().Replace
("KB", "").Trim());
}
Console.WriteLine("Total size of the database is : " + sum + " KB");
}

You might also like