I like to show to our mobile users exactly how much data is in the SQL Compact database. This can give a really quick view of just how much data is on the mobile device and serves as a great tool for quickly providing support to users (e.g to quickly find if the sales rep standing in a field really has sent all sales orders back to to the server). My form normally looks something like this (this is one from a recent Dynamic Nav mobility project) -
The code to do this is as follows -
protected DataTable TableSizes(ref bool haveprefixes) // prefixes is whether we had to split table for clarity on screen
{
DataTable dt = new DataTable();
dt.Columns.Add("Prefix");
dt.Columns.Add("ShortTableName");
dt.Columns.Add("TableName");
dt.Columns.Add("Rows");
// only open connection if its not open already
bool isopen = (cn != null && cn.State == ConnectionState.Open); // cn is a globally defined SQLCeConnection
try
{
if (!isopen) cn.Open();
}
catch
{
if (!isopen) cn.Close();
return dt;
}
SqlCeResultSet reader = this.Sql("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='TABLE' ORDER BY TABLE_NAME"); // this is a shortcut function that just quickly return sqlceresultset
haveprefixes = false;
if (reader != null)
{
while (reader.Read())
{
string strname = reader["TABLE_NAME"].ToString();
DataRow ourrow = dt.NewRow();
ourrow["TableName"] = strname;
string[] elems = strname.Split('$');
if (elems.Length == 2)
{
ourrow["Prefix"] = elems[0];
ourrow["ShortTableName"] = elems[1];
haveprefixes = true;
}
else
{
ourrow["Prefix"] = ""
ourrow["ShortTableName"] = strname;
}
ourrow["Rows"] = 0;
dt.Rows.Add(ourrow);
}
reader.Close();
reader.Dispose();
}
foreach (DataRow ourrow in dt.Rows)
{
ourrow.BeginEdit();
ourrow["Rows"] = SqlQuickValue("SELECT count(1) from [" + ourrow["TableName"] + "]"); // Please see post on SQL images for definition of this.
ourrow.EndEdit();
}
// only closed connection if you had to open it already
if (!isopen) cn.Close();
return dt;
}