Delete all tables from sql ce database

Is it possible to execute a command that removes all tables from sql ce database?

thanks for the help

+3
source share
4 answers

You can use SCHEMA to create a script:

select 'drop table ' || table_name || ';'
  from information_schema.tables;

Then run the script. You may need to run the script several times because SQLCE does not have the CASCADE (AFAIK) option .

+5
source

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

+1
source

, , .

0

:

  • Using the SQL Server Compact / SQLite Toolbox in Visual Studio, right-click the .sdf file and select Script Database> Script Database Schema ...
  • Save it as CreateTables.sqlce
  • Paste the following into the 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 that they are created, so you will not get any dependency errors.

0
source

Source: https://habr.com/ru/post/1772848/


All Articles