sqlmdfldf

Selecting LDF file using sys.master_files


This might be simple but can not find example of selecting LDF file form sys.master_files. I have got the MDF file using

    cmd.CommandText = ("select physical_name from sys.master_files where database_id = DB_ID('" + "mydatabasename" + "')");

Solution

  • The query you are using should actually return all data (MDF) and log (LDF) files for your database.

    However, to return only log files, you simply add an extra criteria on the 'type' column. Log files have a type of 1, hence:

    cmd.CommandText = ("select physical_name from sys.master_files where type = 1 and database_id = DB_ID('" + "mydatabasename" + "')");