是否有在 PowerShell 中创建 LocalDB 数据库(不是实例)的单行程序?

Is there an one-liner to create LocalDB database (not instance) in PowerShell?

我想以 MDF 和 LDF 文件保留在脚本执行文件夹中的方式创建 LocalDB 数据库。

这可能比您要求的要多得多,但它会解决您的问题。

这将询问您是否要从模板恢复,如果否,它将创建一个空数据库。

$backupFilePath=''
$TempDB=''
$TempDBlog=''
$DBName=''
$Logfilepath=''
$Datafilepath=''

$confirmation = Read-Host "Do you want to use a DB template? [y/n]"

If($confirmation.ToUpper().StartsWith("Y") )
{
$dbCommand = "use [master];RESTORE DATABASE [$DBName] " + "FROM DISK = N'$backupFilePath'" + "WITH FILE = 1, NOUNLOAD, STATS = 10, " + "move '$TempDB' to '$Datafilepath" + "\" + "$DBName" + ".mdf', " + "move '$TempDBlog' to '$Logfilepath" + "\" + "$DBName" + ".ldf';" + "alter database [$DBName] modify file (name=N'$TempDB ', newname=N'$DBName" + "_data1');" + "alter database [$DBName] modify file (name=N'$TempDBlog', newname=N'$DBName" + "_log1');" + "alter database [$DBName] set read_committed_snapshot on with rollback immediate;" + "alter database [$DBName] set COMPATIBILITY_LEVEL = 100;"
}

else {
$dbCommand = "use [master];CREATE DATABASE [$DBName] ON " + "( NAME='$DBName" + "_data1', FILENAME='$Datafilepath" + "\" + "$DBName" + "_data1.mdf', SIZE=1GB, MAXSIZE=UNLIMITED, FILEGROWTH=200MB)" + "LOG ON ( NAME='$DBName" + "_log1', FILENAME='$Logfilepath" + "\" + "$DBName" + "_log1.ldf', SIZE=1GB, MAXSIZE=UNLIMITED, FILEGROWTH=200MB); " + "alter database [$DBName] set read_committed_snapshot on with rollback immediate;" + "alter database [$DBName] set COMPATIBILITY_LEVEL = 100; " + "set AUTO_CLOSE OFF;"
}

有人将不得不把它编辑成漂亮的代码中断,因为它不允许我这样做。