3

Is it possible to executa a command which drops all tables from sql ce database ?

thanks for help

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 - I was WAY the hell off since CE is so different from SQL Server
in case someone is still looking in 2015 - updated script could be select 'drop table ' + t.TABLE_SCHEMA +'.['+ t.table_name + '];' from information_schema.tables t;
@torm SQL Compact does not have schemas!
1

System.IO.File.Delete and then run the requires CREATE TABLE scripts

1 Comment

Care to expand on that? So... delete the whole file? I just want to rebuild the database each time I run tests. Deleting the .sdf file seems like a bad idea.
0

You can also drop database, and create it again, but it will erase all your configuration, so may not be the best option.

Comments

0

This is how I do it:

  1. Using SQL Server Compact/SQLite Toolbox in Visual Studio, right-click on the .sdf file and select Script Database > Script Database Schema...
  2. Save it as CreateTables.sqlce
  3. Paste 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;" }
    }
    
  4. 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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.