Hi,
I am new to SQL CE.
How to get the list of tables in a database? In C#.
Thanks for your help...
DW
João Paulo Figueira - 21 Sep 2006 22:16 GMT
Hello master,
Use this:
SELECT * FROM INFORMATION_SCHEMA.TABLES
Regards,
João Paulo Figueira
Device Application Development MVP
> Hi,
>
[quoted text clipped - 5 lines]
>
> DW
master - 09 Oct 2006 08:45 GMT
> Use this:
> SELECT * FROM INFORMATION_SCHEMA.TABLES
Thanks.
Steve B. - 04 Oct 2006 13:06 GMT
Here is code that you can use :
/// <summary>
/// Return the list of user tables
/// </summary>
/// <param name="connection"></param>
/// <returns></returns>
private static string[] EnumTables(
SqlCeConnection connection
)
{
if (connection == null)
throw new ArgumentNullException("connection");
return EnumTables(connection, TableTypeFilter.User);
}
/// <summary>
/// Return the filtered list of tables
/// </summary>
/// <param name="connection"></param>
/// <param name="filter">Specify type of table to list</param>
/// <returns></returns>
private static string[] EnumTables(
SqlCeConnection connection,
TableTypeFilter filter
)
{
if (connection == null)
throw new ArgumentNullException("connection");
SqlCeCommand cmd;
IDataReader reader;
ArrayList result = new ArrayList();
cmd = connection.CreateCommand();
// Configure command
switch(filter)
{
case TableTypeFilter.None:
return null;
case TableTypeFilter.Both:
cmd.CommandText = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES";
break;
case TableTypeFilter.System:
cmd.CommandText = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_TYPE = 'System'";
break;
case TableTypeFilter.User:
cmd.CommandText = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_TYPE = 'TABLE'";
break;
}
bool wasOpened = (connection.State == ConnectionState.Open);
if (!wasOpened) connection.Open();
// Create the datareader
reader = cmd.ExecuteReader();
while(reader.Read())
result.Add(reader.GetString(0));
reader.Close();
reader.Dispose();
if (!wasOpened) connection.Close();
return (string[])result.ToArray(typeof(string));
}
/// <summary>
/// Enumerate table types
/// </summary>
[Flags]
internal enum TableTypeFilter
{
/// <summary>
/// No table is returned
/// </summary>
None = 0,
/// <summary>
/// Only user tables are retrieved
/// </summary>
User = 1,
/// <summary>
/// Only system tables are retrieved
/// </summary>
System = 2,
/// <summary>
/// Both user and system tables are retrieved
/// </summary>
Both = 3
}
> Hi,
>
[quoted text clipped - 5 lines]
>
> DW