Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6815959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:52:08+00:00 2026-05-26T20:52:08+00:00

My app installs SQL Server Express Have many .mdf and .ldf files on a

  • 0
  1. My app installs SQL Server Express
  2. Have many .mdf and .ldf files on a disk
  3. app copies all the databases and log files to SQL Server data directory
  4. app attempts to attach the databases programmatically.

My code is:

FileInfo mdf = new FileInfo(dbfile);

databasename = mdf.Name.ToLower().Replace(@".mdf", @"");
StringCollection databasefiles = new StringCollection();
databasefiles.Add(mdf.FullName);
databasefiles.Add(mdf.FullName.ToLower().Replace(@".ldf", @"")); 
//this is where I have issue. Obviously I can't assume that the log file name would be the same as mdf file name with ldf extension. Thats when I thought there would be a way to read the header information from mdf file, and that will have ldf information.

Server sqlServer = new Server(textServer.Text);
sqlServer.AttachDatabase(databasename, databasefiles);

How do I pick the correct .ldf file. For eg in c:\temp I have db1.mdf, db2.mdf and db1.ldf, db_1.ldf, db1_log.ldf. How would I know which is the right .ldf file for db1.mdf

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:52:08+00:00Added an answer on May 26, 2026 at 8:52 pm

    No, as far as I know, there’s no real “link” between the MDF and LDF file in the files per se. There is a link – in the database metadata inside SQL Server.

    My approach would probably be:

    • check if mydatabase.ldf exists -> if yes, use that
    • check if mydatabase_log.ldf exists -> if yes, use that
    • check if mydatabase_1.ldf exists -> if yes, use that

    and if you still haven’t found your LDF file – you could probably do a search for mydatabase*.ldf in the directory where your MDF is located. That would fail miserably on our test servers – our sysadmins always installed the .MDF/.NDF data files into a SQL2008-DATA and the .LDF log files into a SQL2008-LOG directory – totally separate.

    In that case, you would either need to configure your mapping .MDF -> .LDF in e.g. a config file or something, or just pop up a “Find the .LDF file” dialog to the user….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET 3.5 app with a SQL 2005 Express .mdf database file
We have an app that currently installs itself into 'program files\our app', and it
I have a setup project that installs my app to the typical program files
I have an instance with a certain name in SQL Server Express 2005, and
I have just installed the MS SQL Server 2005 Express and, of course, I
anyone have script or procedures to install SQL Server 2008 Express, set up the
I have used SQL SERVER 2005 & c# .net for front end & app.config
I have built a web setup project in VS2008 which installs my ASP.NET/Silverlight app
I'm creating an install package using InnoSetup and installing SQL Server 2005 Express. Here's
I use Visual Web Developer Express Edition 2008 and SQL Server 2008 Express edition.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.