0

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

Taryn
  • 242,637
  • 56
  • 362
  • 405
Cute
  • 13,643
  • 36
  • 96
  • 112

3 Answers3

4
 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.

C-Pound Guru
  • 15,967
  • 6
  • 46
  • 67
0

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

see link text

flurbius
  • 957
  • 7
  • 14
0
$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.

Mouffette
  • 732
  • 1
  • 7
  • 19