如何在 C# 中使用 smo 查找我的数据文件和日志文件的存储位置

发布于 2024-07-25 05:31:06 字数 42 浏览 2 评论 0原文

谁能告诉我如何在 C# 中使用 SMO 查找数据和日志文件存储的位置?

Can anybody please tell me how to find the location where my data and log files stored using SMO in c#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒人说梦 2024-08-01 05:31:06
 public static void foo() {
     Microsoft.SqlServer.Management.Smo.Server server = new ServerConnection("<server name>");
     Microsoft.SqlServer.Management.Smo.Database db = server.Databases["<database name>"];
     Console.WriteLine(db.FileGroups[0].Files[0].FileName);
     Console.WriteLine(db.LogFiles[0].FileName);
  }

此示例假设您对 Server\Database 有足够的权限,并且仅返回文件组中第一个 db/log 文件的完整路径\文件名。

文件组、文件和日志文件是 SMO 集合,将包含一个或多个各自的项目。

 public static void foo() {
     Microsoft.SqlServer.Management.Smo.Server server = new ServerConnection("<server name>");
     Microsoft.SqlServer.Management.Smo.Database db = server.Databases["<database name>"];
     Console.WriteLine(db.FileGroups[0].Files[0].FileName);
     Console.WriteLine(db.LogFiles[0].FileName);
  }

This example assumes you have sufficient rights to the Server\Database, and only returns the full path\filename for the first db/log file in the filegroup.

FileGroups, Files, and LogFiles are SMO collections that will contain one or more of it's respective items.

影子的影子 2024-08-01 05:31:06

显然,您可以“从文件组和文件集合中获取逻辑数据和日志文件的名称”。

请参阅链接文本

apparently you can "get the names of the logical data and log files from the FileGroups and the Files collections."

see link text

瑾夏年华 2024-08-01 05:31:06
$targetServerName = "localhost"
$targetDatabaseName = "dbname"

$targetServer = New-Object ("Microsoft.SqlServer.Management.Smo.Server")$targetServerName 
$database = $targetServer.Databases[$targetDatabaseName]

foreach ($fg in $database.FileGroups)
{
  foreach ($df in $fg.Files)
    {
    "Filegroup type : " + $fg.Name + " DataFiles : "  + $df.FileName

    }
}
foreach ($lf in $database.LogFiles)
{
   "Log file : " + $lf.FileName
}

这应该会给你确切的路径。

$targetServerName = "localhost"
$targetDatabaseName = "dbname"

$targetServer = New-Object ("Microsoft.SqlServer.Management.Smo.Server")$targetServerName 
$database = $targetServer.Databases[$targetDatabaseName]

foreach ($fg in $database.FileGroups)
{
  foreach ($df in $fg.Files)
    {
    "Filegroup type : " + $fg.Name + " DataFiles : "  + $df.FileName

    }
}
foreach ($lf in $database.LogFiles)
{
   "Log file : " + $lf.FileName
}

That should get you the exact paths.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文