Is it possible to executa a command which drops all tables from sql ce database ?
thanks for help
You can use information SCHEMA to generate a script:
select 'drop table ' || table_name || ';'
from information_schema.tables;
Then run that script. You might need to run the script several times, since there is no CASCADE option (AFAIK) in SQLCE.
System.IO.File.Delete and then run the requires CREATE TABLE scripts
This is how I do it:
CreateTables.sqlcePaste the following in a PowerShell console:
function GenerateDropScript($DbSchemaFile)
{
function Reverse($inputArr)
{
$arr = @($inputArr)
[array]::Reverse($arr)
return $arr
}
$TableNames = Get-Content $DbSchemaFile |
? { $_.StartsWith('CREATE TABLE') } |
% { $_.Substring(14).Replace('] (', '') }
$ReverseNames = Reverse($TableNames)
"Generating DROP script for all tables in: `n`t$DbSchemaFile`n"
$ReverseNames | % { "DROP TABLE [$_]; GO;" }
}
Run GenerateDropScript CreateTables.sqlce
This will read the CREATE script and generate DROP scripts for all tables in the reverse order they are created, so you don't get any dependency errors.