Read file info using SMO locks database!

Hi!

I'm using SMO from C# to restore a database from a backup file. Since I want 
to re-use the same datafiles I try to read the datafiles in my destination 
database and call RelocateFile to use these filenames during the restore. 
Everyting works fine except when I try to read the current datafilenames 
(see code below). If I skip this code and just hardcode the paths in 
relocatefile everything works fine.

Database db = svr.Databases[destinationDBName];
foreach (FileGroup group in db.FileGroups)
  foreach (DataFile file in group.Files)
                    Console.WriteLine(file.FileName);


The problem is not to read the filenames but when I later on do the actual 
restore

res.SqlRestore(svr);

I receive the following error
{"System.Data.SqlClient.SqlError: RESTORE cannot process database 'MYTESTDB'
because it is in use by this session. It is recommended that the master
database be used when performing this operation."}

I have tried to set the initialcatalog to master when I connect to the 
database but I still get the same error message. As soon as I touch the 
svr.Databases the database gets locked.

Regards
/Hans


0
Hans
4/6/2010 8:04:38 AM
sqlserver.programming 1873 articles. 0 followers. Follow

1 Replies
701 Views

Similar Articles

[PageSpeed] 18

For anyone interested it seems that when you access the FileGroups/Files 
collections SMO is doing an implicit "use [database]" which locks that 
database. I tried to solve this earlier by doing

Database db = svr.Databases[destinationDBName];
foreach (FileGroup group in db.FileGroups)
  foreach (DataFile file in group.Files)
                    Console.WriteLine(file.FileName);
db = svr.Databases["master"]  //Added this dummy row

but that was not enough. You also had to access the db.FileGroups collection 
like

Database db = svr.Databases[destinationDBName];
foreach (FileGroup group in db.FileGroups)
  foreach (DataFile file in group.Files)
                    Console.WriteLine(file.FileName);
db = svr.Databases["master"]  //Added this dummy row
string dummy = db.FileGroups[0].Name;


After I have added the "string dummy = db.FileGroups[0].Name;" the lock on 
my destionationDB was released.

Regards
/Hans 


0
Hans
4/8/2010 1:07:04 PM
Reply:

Similar Artilces:

.qfx file in excel
my bank for some reason will only allow me to download my statement in a qfx file (quicken) but i dont have any quicken programs i use excel to manage my finances how can i open this file up??? If the bank does not have .text (.txt) or comma separated variables (.csv), which would probably be listed as spreadsheet format, then I would complain to the bank. Meanwhile you could copy and paste from your checking activity HTML pages. --- HTH, David McRitchie, Microsoft MVP - Excel [site changed Nov. 2001] My Excel Pages: http://www.mvps.org/dmcritchie/excel/excel.htm Search Page: ...

.mdi Files
Version: 2008 Operating System: Mac OS X 10.5 (Leopard) Processor: Intel I have some .mdi files (generated at work with some lab software). They seem to open okay on my work PC. Is there any way that I can get Office 2008 and a MAC to recognize / open these? No, .mdi is a Windows-only format exclusive to the Microsoft Document Imaging program... Even other Windows apps can't read or create them. If you can get them into a Windows box open them in MDI & then Save As TIFF to get a format that can be opened by graphics apps or inserted into other documents as a picture. TIFF files are ...

Using Outlook 2003 in Windows 7
I have a valid version of Outlook 2003 that I am using in Windows 7 that has been properly activated. However, every time I open it in Windows 7 I am asked to accept the Microsoft End User Agreement, which is annoying since I accept it every time (actually this happens every time I open an Office 2003 product). Is there someway to deactivate this request? Thx for the help. "Barry" <Barry@discussions.microsoft.com> wrote in message news:11F6EC50-7351-4B84-A7BC-4322115C9B88@microsoft.com... > I have a valid version of Outlook 2003 that I am using in Win...

Unable to start up my Excel files
One of my users have this odd issue with his Excel 2003. He's unable to start the files directly but instead he must start a blank excel sheet then open his files from there. Is there a solution to this issue? Please advise. Thanks! Dell 170L P4 800mghz 1gbram Windows XP Pro SP2 Office 2003 SP2 Could the problem be that he cannot open files by double-clicking on them from Windows Explorer, but must open Excel (which will launch a blank sheet) and then open them directly from there using File / Open... If so then tell him to try Tools / Options / General / untick 'Ignore Othe...

File password override
I'm trying to restore a backup file. Money is asking for a file password. I enter the password that is appropriate for the Passport account used to lock the file, but Money will not accept that. I wonder if this file has a seperate file password and if so, I have no memory of what it may have been. Is there some way to reset it? Is there some other way in to this file? Thanks Though I sympathize with your problem, I certainly hope there's no back door. If there is, I'm going back to paper records. I shudder to think that a hacker could conceivably get an older backup file an...

How to use the countif() function to count items in a table
I have a table contains multple columns of info as shown in the below AcctID ProjectName PMName Phase Status EIG Project 1 Wellie Initiation New MLI Project 2 John Develop Active EIG Project 11 Wellie Qualify Active EIG Project UUU John Design InActive MLI Project XX Carol Deployment Completed MLI Project YYY Carol Design Active I'd like to use the countif() function to complete the following metric such that I know how many projects for each status for each AcctID. EIG ...

opening .dat Accounts Receivable file in Great Plains for DOS
Hi There: We are trying to retrieve some information from a posted accounts receivable report... We think its in a .dat file but have no way of opening... This anyone know if this is possible... Any help or suggestions would be greatly appreciated! Sincerely, Carmine Carmine, Can you be a little more specific? What version of GP, what report, what information? Victoria "Carmine" <Carmine@discussions.microsoft.com> wrote in message news:839CC67C-A4C7-4584-8119-C40F74960573@microsoft.com... Hi There: We are trying to retrieve some information from a posted accounts ...

Where is Read All Unread Mail ?
Using Outlook 2003, had to re-create my profile due to corruption. And now I no longer have a Folder title to display ALL UNREAD MAIL. Does this need to be created somehow, so that it will display all unread mail spread out in all the folders? if the folder is not in the search folders, right click on Search Folders and choose New. Its one of the predefined options. -- Diane Poremsky [MVP - Outlook] Author, Teach Yourself Outlook 2003 in 24 Hours Need Help with Common Tasks? http://www.outlook-tips.net/beginner/ Outlook 2007: http://www.slipstick.com/outlook/ol2007/ Outlook Tips by...

csv-file problems with different regions
I have imported an American csv-file into a German Excel XP. The American $-values are separated by a "." while the German currency values are separated by a ",". So now my spreadsheet reads a value of $ 18.01 as 18. January. All values above 18.12 are ok. How can I avoid this problem? Or, can I change the values tediously one cell after the other? I have not found a way yet as even when I try to format a cell user defined it automatically changes to the format used in the German region. Thanks. Hans Temporarily change the regional settings on your computer to the same f...

Corrupt TCP segments using CSocket
I am developing a simple streaming video application. I am using TCP with the CSocket MFC class and I have a thread the writes the data for each frame in a separate Write(). The app works fine for about 2 minutes of "playing" the video, but after some time (2-15) minutes I am getting segments with invalid TCP checksum (observing the traffic in etherreal). If I retry the connection from the client it doesn't recover. It recovers when I restart the server. Any ideas? Ask your question additionally at microsoft.public.win32.programmer.networks NG. -- Vladimir <vladimir.rale...

File set up query.
Can i set up an excel file to open automatically on pc start up? The easiest is to use the Startup folder available in the Start Menu. To access it go to Start - Programs - Startup. You can delete or add tasks here according to your needs. They will start after Windows logon. When you start Excel, all the files that are located in the XLStart folder are automatically opened. Additionally, if you specified an alternative startup folder for Excel, every file in that folder is also automatically opened. To specify an alternative startup folder, in Microsoft Excel 2002 or Microsof...

Using Office 2004 student teacher edition on multiple computers on a local network
Even though each copy of Office 2004 has it's own product number, I can only have an Office program open on 1 computer on my home network. This started after upgrading to 10.5. Anyone have a suggestion for dealing with this? rwblackwood@mac.com wrote: > Even though each copy of Office 2004 has it's own product number, I > can only have an Office program open on 1 computer on my home network. > This started after upgrading to 10.5. Anyone have a suggestion for > dealing with this? If you've installed the same key on all the machines then start the Remove Office tool f...

Sorting file list by date/time...
Hello, does anybody have an idea how to program a sorting function given a directory with files, which should red to an array which is sorted by date and time, but contains only the filenames. Is there a MFC function or a shell function of the OS which could ease the job instead of doing it manually? Regards, Diana On Mon, 16 Apr 2007 16:39:50 +0200, "Diana Bulthaupt" <Diana.Bulthaupt(at)web.de> wrote: >does anybody have an idea how to program a sorting function given a >directory with files, which should red to an array which is sorted by date >an...

whihc microsoft office can i use to determine loan amortization
I am looking for programs wherein I can determine loan amortization "Justin Reyes" <JustinReyes@discussions.microsoft.com> wrote in message news:304D4935-F597-4BE9-A290-59C78A19AEBC@microsoft.com... |I am looking for programs wherein I can determine loan amortization You need Excel, not Access -- Len ______________________________________________________ remove nothing for valid email address. ...

Can I access the previous version of a saved file?
I overwrote an excel file by mistake, using the save as command to an incorrect file. Is there any way to recover the previous file before I "saved as" over it? Hi! I'm using Excel 10 (XP)......WinXP....... If I have an existing file and open it, make some changes, then do a SAVE AS and give it a different name, the newly "saved as" file is a completely *NEW* file and the original is still in tact with the previous file name. Try looking for the previous file name. Biff "JCC" <JCC@discussions.microsoft.com> wrote in message news:9CAD1748-1D5C...

Form Locked !
Hi, I created a form using the wizard based on a query, everything looks ok but i cannot enter data into the form as it appears to be locked ? what have i done wrong, i been trying to solve this for 2 hours now - please can you HELP!!! blake7, One common reason is the query used for the form. Open the query and see if you can add a new record to the query. If you can't, have a look here: http://www.allenbrowne.com/ser-61.html Jeanette Cunningham "blake7" <blake7@discussions.microsoft.com> wrote in message news:808F403F-8822-4619-9F50-DCA5E8B57DC1@microsoft.com... ...

database growth after a brick level restore
We had a disasterous drive failure and only had mailbox level incremental backups (don't ask). After restoring, there were thousands and thousands of duplicates and my databases had tripled in size. We ran some dup removal tools and they seem to have done a fine job of removing the duplicate messages/contacts/events, but my databases are still huuuuuge. Not quite as huge as before, but nowhere near their svelte original size. Any suggestions? the important info: Exch 2003 Ent on W2k3 Standard all the latest SP's, on Dell Servers. Thanks a meeelion Ben You'll have to do an ...

how to edit Adobe file
how to edit Adobe file Most certainly not in Word. pdf files, I'm assuming that is what you mean by 'Adobe files', are 'fixed' files. Their layout is frozen so they look the same on any machine and can not be edited. If you still want to edit such a pdf file, you will have to convert the file to something else, edit it, and then save the result back as a pdf. The best tool to do the conversion of pdf files to Word documents is probably Acrobat Professional. Unfortunately, that is a rather expensive product to use for a few simple conversions. There are ...

how do i configure outlook to use my aol email account
jonbon wrote: <nothing> PLEASE write your question in the body of the post and NOT just the subject Have a look here: http://dts-l.org/goodpost.htm the answer: http://www.slipstick.com/addins/services/online.htm which Google would have told you with 6.5 million hits in 0.43 secs. http://www.slipstick.com/addins/services/online.htm#aol -- Diane Poremsky [MVP - Outlook] Author, Teach Yourself Outlook 2003 in 24 Hours Coauthor, OneNote 2003 for Windows (Visual QuickStart Guide) Need Help with Common Tasks? http://www.outlook-tips.net/beginner/ Outlook Tips: http://www.outlook-t...

Print Preview using CPreviewDC
Hi there, I have a problem trying to implement a print preview function. Yet the printing itself is no problem. I have this chart control with a method m_graph.PrintGraph(CDC *pDC) which outputs the data to the CDC object. Now I want to do Print Preview similarly using the CPreviewDC class mentioned in the MSDN Documentation instead of the CDC class. How do I setup the CPreviewDC object without a print dialog? And after I have printed the content into the CDC class how do I display the print preview? ...

using min max
We want to start using min max to help with order processing. Do we need to use order point qty or order up to level with the min max? We are using GP v10 and new to GP. Do we need to set the order policy before using min max? Is there a good source on how to set the min max in GP10? the Purchase Order Processing pdf in the GP Documentation folder is pretty good. The values are all interelated. What we did here was create a dummy part and played with it. You can leave the Items order policy as 'Not Planned'. We wanted to set up to use PO generation but our purchasing g...

restore exchange 2000 using ntbackup
Hi! My exchange 2000 server is remotely backed up every day using veritas 9.2. I am now testing disaster recovery and would like to use ntbackup locally on my disaster recovery exchange server. (I copied my .bkf file on my exchange server) I dismounted my databases and ran ntbackup locally on my exchange server. I cataloged the bkf file and selected the microsoft Information store that I want to restore. „Restore to „ I entered my exchange server name. The following error message is then displayed: „the specified computer is not a Microsoft Exchange server or ist Microsoft Excha...

How many recordset can be opened simultaneously for a MS-Access 2000 Database for adding /editing records
Hey Friends Can someone please suggest how many recordset can be opened simultaneously for a MS-Access 2000 for adding /editing records. I also want a comparison with MSDE for the same. Vikrant wrote: > Hey Friends > Can someone please suggest how many recordset can be opened > simultaneously for a MS-Access 2000 for adding /editing records. > I also want a comparison with MSDE for the same. Uhm... If I remember correctly that would be max. 20 connections. Successive CRecordset::Open(...) requests would fail. I don't know this parameter for MSDE. There's a limit in...

forms not listed in database contents, yet can add data to them
Hi - there are no forms listed when I open up the database in design view. All the tables, queries, and macros are there. When I open up the database to add data, all the forms are there and can be used. The data is stored. So, it seems that the forms are still there, but just cannot be seen in design view. The database, when I open it up in design view, says Access 2000 file object even though I am using Access 2003 - could that be the problem? Thanks! You cannot open an entire database in Design View. Try opening it normally, selecting a form, and choosing Design View from the V...

Replicating Formulas between excel files
Hi there, Does anyone know how I can replicate formulas (verbatim) between difference excel files? I've tried to cut and paste between the separate files, but to my dismay it makes a reference to the original file. Regards, Nickchups The best procedure depends on how many you're trying to copy. For one or two, you can select the *entire* formula in the formula bar, right click and choose "Copy", then hit <Enter>. Now you can navigate to wherever you wish, and Paste it as many times and/or places you want. For numerous formulas, *unformulate* them by replacing t...