DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Snowflake Administration: A Comprehensive Step-by-Step Guide
  • Useful System Table Queries in Relational Databases
  • How to Restore a Transaction Log Backup in SQL Server
  • How to Attach SQL Database Without a Transaction Log File

Trending

  • Designing AI Multi-Agent Systems in Java
  • A Guide to Using Amazon Bedrock Prompts for LLM Integration
  • Knowledge Graph Embeddings and NLP Innovations
  • Power BI Embedded Analytics — Part 3: Power BI Embedded Demo
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Resolve SQL Server 'Database cannot be Opened' Issue Due to MDF Corruption?

How to Resolve SQL Server 'Database cannot be Opened' Issue Due to MDF Corruption?

Resolve the "Database cannot be opened" issue in SQL Server caused by MDF file corruption with methods like backup restoration, DBCC CHECKDB, or repair tools.

By 
Priyanka Chauhan user avatar
Priyanka Chauhan
·
Jan. 03, 25 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
3.3K Views

Join the DZone community and get the full member experience.

Join For Free

Master Database File (MDF) is the primary database file in MS SQL Server that stores all the data, including views, tables, stored procedures, foreign keys, and primary keys. Sometimes, while opening the MDF file, you may face an issue where the SQL Server database cannot be opened and encounter an error, like the one given below:

  • SQL Server error 5171: MDF is not a primary database file
  • Unable to open the file xxxxx.mdf. Operating system error 5: (Access is denied)”
  • Event ID 824: SQL Server detected a logical consistency-based I/O error
  • Msg 8114, Level 16, State 5, Line 2: Error converting data type varchar to numeric

Such errors usually occur when there is corruption in the MDF file. In this article, we'll explain the possible reasons behind the corruption of MDF files and show how to repair corrupted MDF files and resolve the issue.

Causes of Corruption in the MDF File

There are various reasons that can lead to corruption in the SQL Server database (MDF) file. Some common ones are given below:

MS SQL Server or System Crash

MS SQL Server or system may crash due to issues with the operating system, hardware, or software. If the server or system crashes when working on the MDF file, it can damage or corrupt the file.

Abrupt System Shutdown

Sudden power failure or forced shutdown of the system while the SQL Server is running can corrupt the MDF file stored on your system.

Malware or Virus Attacks

Malware or viruses can also damage MDF files and make them inaccessible. It usually occurs when your system does not have an antivirus or anti-malware protection software.

Issues With Storage Drive

Bad sectors on the storage drive or file system errors can corrupt the SQL database files. If the drive is damaged, then it can also cause corruption in the MDF files.

Methods to Resolve 'Database cannot be Opened' Issue Due to MDF File Corruption

If your MDF file is corrupted, you can then follow the below-mentioned methods to repair and recover the corrupt MDF file, thus resolving the “Database cannot be opened” issue in SQL Server.

Method 1: Restore the MDF File From Backup 

If you have created a backup of your MDF file, then you can restore the file from the backup. But before restoring, you need to check if your backup is complete and readable. For this, you can use the RESTORE VERIFY ONLY command. If this command shows a success message, then you can proceed to restore the backup. For this, use the below Transact SQL command:

MS SQL
 
BACKUP DATABASE [AdventureWorks2019] TO DISK = N’C:\backups\AdventureWorks2019.bak’ WITH NOFORMAT, NOINIT, NAME = N’AdventureWorks2019-Full Database Backup’, SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO


Alternatively, you can use the SQL Server Management Studio (SSMS) to restore the backup.

Method 2: Use DBCC CHECKDB Command

If the backup is not updated or there is a problem while restoring the backup file, then you can use the DBCC CHECKDB command to repair the corrupt MDF file. To repair the database file, you need to make sure that you have admin rights on the database. Before repairing, set the database to single-user mode by using the below command:

MS SQL
 
ALTER DATABASE Dbtesting SET SINGLE_USER


If you are unable to set the database to SINGLE_USER mode, then verify that the AUTO_UPDATE_STATISTICS_ASYNC option is set to OFF. 

Next, run the DBCC CHECKDB command as given below to repair the database (MDF) file: 

MS SQL
 
DBCC CHECKDB (N ’Dbtesting’, REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS, NO_INFOMSGS

GO


When the database is repaired, set it to MULTI_USER mode again by using the below command: 

MS SQL
 
ALTER DATABASE Dbtesting SET MULTI_USER


Note: The DBCC CHECKDB command can help you repair the MDF file. However, while repairing the file, it may deallocate pages or rows, which can result in data loss. 

Method 3: Use a Professional SQL Repair Tool

Repairing the SQL database using the DBCC CHECKDB command with the REPAIR_ALLOW_DATA_LOSS option can result in data loss. To avoid data loss, you can use a powerful MS SQL repair software, like Stellar Repair for MS SQL. This software can repair corrupt MDF files and restore all the file objects, like primary keys, tables, triggers, views, etc., without changing the original structure. It saves the repaired file data in a new MDF file and various other formats, such as CSV, HTML, and XLS. The software supports SQL database (MDF and NDF) files created on Windows and Linux systems. 

To Conclude

The 'Database cannot be opened' issue in MS SQL Server can occur due to corruption in the MDF file. To fix this issue, the easiest way is to restore the MDF file from the last backup. If the backup file is obsolete or not working, you can use the DBCC CHECKDB command to repair the corrupt MDF file. If the DBCC CHECKDB command fails to repair the database, then use a powerful SQL repair tool, like Stellar Repair for MS SQL. It can repair corrupt MDF files and restore all the data to a new MDF file with complete integrity. You can install the free version of Stellar Repair for MS SQL to scan the corrupt MDF file and preview the recoverable data.

Database sql Data corruption Data recovery

Opinions expressed by DZone contributors are their own.

Related

  • Snowflake Administration: A Comprehensive Step-by-Step Guide
  • Useful System Table Queries in Relational Databases
  • How to Restore a Transaction Log Backup in SQL Server
  • How to Attach SQL Database Without a Transaction Log File

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!