Using PowerShell to manage SQL Server Compact database files

I noticed that someone asked if you could manage SQL Server Compact from PowerShell, and yes you can, based on the reply here I put together the following sample:

[Reflection.Assembly]::LoadFile(“C:Program FilesMicrosoft SQL Server Compact Editionv3.5DesktopSystem.Data.SqlServerCe.dll”)

$connString = "Data Source=C:datasqlcetestroads.sdf"
$cn = new-object "System.Data.SqlServerCe.SqlCeConnection" $connString

# create the command
$cmd = new-object "System.Data.SqlServerCe.SqlCeCommand"
$cmd.CommandType = [System.Data.CommandType]"Text"
$cmd.CommandText = "SELECT TOP (100) * FROM Road"
$cmd.Connection = $cn

#get the data
$dt = new-object "System.Data.DataTable"

$rdr = $cmd.ExecuteReader()


$dt | Out-Default | Format-Table

Write a Comment

Your email address will not be published. Required fields are marked *