soiryk139
soiryk139

Reputation: 85

Opening .mdf file without ldf in Visual Studio 2022

I am trying to open a .mdf file using Visual Studio 2022 and create a database in local machine.

I ran the following query in Visual Studio

CREATE DATABASE MKT2024
    ON (FILENAME = N'C:\Users\b123\OneDrive - Test\Desktop\MKT_EDM.mdf')
    FOR ATTACH_REBUILD_LOG
    GO

I get the following error:

Msg 1853, Level 16, State 1, Line 1 The logical database file 'MKT_EDM_log' cannot be found. Specify the full path for the file. Msg 1813, Level 16, State 2, Line 1 Could not open new database 'MKT2024'. CREATE DATABASE is aborted.

Is there a way to open the .mdf without the ldf file as this is not provided? I don't have administrator rights as this is work machine. Appreciate your guidance. Thank you.

Upvotes: 0

Views: 119

Answers (1)

Jhonty
Jhonty

Reputation: 303

Please try below commands to work with .mdf

EXEC sp_attach_single_file_db @dbname = 'MKT2024', 
                              @physname = 'C:\Path\To\MKT_EDM.mdf';

if above command doesn't work then try this:

CREATE DATABASE MKT2024
ON (FILENAME = 'C:\Path\To\MKT_EDM.mdf')
LOG ON (NAME = 'MKT_EDM_log', FILENAME = 'C:\Path\To\NewLog.ldf')
FOR ATTACH;

This command creates the new log file on the specified path.

Make sure you have rights to access mdf file. To allow rights follow the below steps:

  1. Right-click the .mdf file. select Properties.

  2. Inside Security tab click Edit and then Add to include the SQL Server service account.

  3. Grant Read, Write, and Modify permissions.

Upvotes: 1

Related Questions