- My app installs SQL Server Express
- Have many .mdf and .ldf files on a disk
- app copies all the databases and log files to SQL Server data directory
- 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