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
733 Views

Similar Articles

[PageSpeed] 55

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:

Money 99 -> Money 2004 File conversion
I have Money 1999 (Ver 7), with data dating back to 1995. I have downloaded same region Money 2004 (M12GBWEB.exe). The file conversion does not complete properly, ending in a core dump. The same problem exists with Money 2003 (M11GBWEB.exe). If I archive records from my Money 1999 file removing - say - all records prior to 2000, I can load the resulting data. However, I would like to keep all the data back to 1995 not just that since 2000. Any ideas would be gratefully received. Kind regards, David Goddard See reponse in UK group. -- Glyn Simpson, Microsoft MVP - Mon...

Viewing a file before it is open
There is an option under File, Open called preview. Yet I can not view any excel files. It always shows "Preview not available" Is there a setting that I need to change to be able to do this? You must save the file with the "Save preview picture" enabled for that file. There is no global save function that I know of. With the file open go to File>Properties>Summary. Checkmark option at lower left. Save the file. Gord Dibben MS Excel MVP On Tue, 5 Sep 2006 17:41:02 -0700, William <William@discussions.microsoft.com> wrote: >There is an option und...

Publisher catelog merge- cannot open data source file
whether I try to open the source file or open a new one, I get the message: operation cannot be completed becasue of dialog or database engine failures. Please try again later. Troubleshooting advice suggestd to undate the engine from Windows update - can't find it. Does anyone have more specific troubleshooting advice? This article has a solution: You receive a "The operation cannot be completed because of dialog or data base engine failures" error message when you try to perform a mail merge by using an Outlook 2003 contact folder in Publisher 2003 http://support.mic...

New Database in Storage Group question
I think I have some corruption in an exisiting database in my Storage Group. What I am doing is moving all mailboxes to a newly created database within this storage group. Is this a good thing to do? I was figuring instead of taking the corrupted database (I am getting some 1025 errors) down for 50+ hours while I run isinteg, I could just move the mailboxes to a new database. Will this work? Since I am allowed 5 databases within a storage group, what is Microsoft best practices when it comes to database size? -- Eric Sabo NT Administrator Best practice is to keep the size ...

Can't add files to Media Player!
I'm getting REALLY frustrated with MP11! It's easy to go online and find audio files but I can't figure out how to add a bunch of mp3 files I've created on my computer! Where's HELP in the program? I told it to check the directory where I have these files, but that's not working! There's no menu option to "add files??" Arrgh! On Thu, 21 Jan 2010 08:41:01 -0800, Bob Munk <Bob Munk@discussions.microsoft.com> wrote: >I'm getting REALLY frustrated with MP11! It's easy to go online and find >audio files but I can...

I used Omsgclas.exe Utility and my contacts don't use then new form
I created a new form for contacts. I followed the three steps in the document How to Update Existing Items to Use a New Custom Form. I can click on Action and do new contact and the new form is used. I used Omsgclas.exe utility to change the message class of the contacts. It shows as changed. However, when I click on an old contact (previous to changing the form), A message form comes up (not the old contact form and not my new contact form). Anyone have any help how to get my new form pulled up for old messages? Note: I had a form with a ' in the name, I made a new form w...

Slightly OT: Anyone ever used the IDiscMaster interface?
I am getting "class not registered" errors on CoCreateInstance even though the service is up and running on my machine, and I have the Platform SDK (ergo, "Imapi.h" where the CLSIDs are defined.) 'lil help? WTH Doh, guess what? LOL, IMAPI runs as a local server executable. So, if you want to use it, specify LOCAL_SERVER instead of INPROC_SERVER... Sorry to have wasted everyone's time. WTH "WTH" <ih8spam@spamtrap.com> wrote in message news:OsU3sorsDHA.1888@TK2MSFTNGP10.phx.gbl... > I am getting "class not registered" err...

Please Help! Messed up Hard Drive from using Partition Magic
I wanted to use Partition Magic to partition my hard drive, so I used the boot CD. When I loaded it up, it said that there were 2 errors in my hard drive and it could fix them, so I pressed fix. Unfortunately, I was running Vista and I only know now that they're incompatible. However, now my hard drive is completely screwed! If I plug my hard drive into my computer, my computer refuses to do anything after POST, it doesn't boot a CD or run anything from the hard drive. I unplugged my hard drive and it's booting CDs now, even though my CD was the primary boot device anyway...

Re-using a "Door Prize No"
Hi, I’m on Access 2003 on XP Pro with all the latest updates. I’m working for a charity that has a membership database. Each member is assigned a door prize number. The goal is to not have any gaps in the door prize number. When a member leaves the area, we flag that member as “gone” and I want to re-assign their door prize number to the next new member. I’ve already beat my head against the wall talking to management about why they reuse number and they are not going to budge. I can logically see what I need to do, but I don’t know how to code it. I would appreciat...

missing excel VBA help file
I'm missing the excel 97 VBA help file "VEenUl3.hlp". I assume this file is on the software distribution pack that came with my computer. If it is do I reinstall excel or can I just copy the help file from the distribution pack? Finally I may not have the distribution pack since I've moved three times in the five years since the computer was purchased. How would I get a copy if I need it? I'm using windows 98. Thanks for your help John VBA and help files are not part of normal Office install. ie. Office 9 or Excel 97 *not* Windows disk required. Perhaps you ...

How do I read Pocket Excel files from Excel
Just got an Ipaq and trying to use ActiveSync to synchronise speadsheet files between that and my desktop. Unfortunately, when I update the file using Pocket Excel on the Ipaq, it is converted to .pxl format, which the desktop version of Excel (2000) won't read! How do I get round this one? ...

Can't open workbook by clicking on file
Why won't double-clicking open the workbook? When I am in Windows Explorer (or other folder) with excel file in it I double-click on the file and xcel opens WITHOUT a workbook! If I have one or two workbooks open and double click on the nex workbook I want to open, it still opens a blank xcell window, not blank workbook, a blank xcell window. I am runningwindows XP. It did this with office 2000, and now afte installing Office XP 2004 it STILL does it. The only way to open a workbook is to open excel first and then us open command and navigate. Sounds minor, but can't do merge i...

counter problem(you really want to read this)
i have a workbook that contains two sheets. Sheet one contains a list of names. Each name references an information sheet for that name. I wish to collect data for each name for a specified number of days "x". When that number of days is met, then I wish to click on a button on the toolbar to delete some number "x" of days of data from 1 - "x" including "x". I wish to do this to make the data more current hence meaningful ...

Converting Publisher Files to Acrobat
Is it possible to convert a Publisher file (2-page newsletter -- mostly text) to Acrobat? How do I do this? THANKS. Please respond directly to my email -- LSPosey@pdq.net You will need a third party program to do it. Here's my canned response. You will need a program to convert the file to a pdf format. There is of course Adobe Acrobat but its a bit on the spendy side for most folks at $250 or higher. I had the need to create pdf files some 5 years ago and did some trials on freeware, shareware and commercial programs (excluding Adobe Acrobat). The winner for me was/is the commercial ...

Could not convert Quicken file!
I've just installed Money 2004, and tried to convert my Quicken 2003 file to be used in Money. After trying to convert the file I get the message: "Your Quicken file could not be converted." What's going on? How do I convert the file? Don J In microsoft.public.money, Don J wrote: > I've just installed Money 2004, and tried to convert my Quicken 2003 >file to be used in Money. After trying to convert the file I get the >message: > > "Your Quicken file could not be...

GP Security output xml file contents and relations
I've exported the xml file from GP showing all forms, reports etc. that are currently operating within the instance of GP. The xml file contains resource id's, names, series types and other information. I'm trying to create a relation between the exported xml file and the SYO1400 table in GP. I believe, through some sql hoops, I've been able to retrieve all the permissions or 'resource' objects a user DOES HAVE permission to. I would like to know, if anybody does know, how the 'explorer', 'tool' and 'posting' nodes within the xml file r...

Converting Mac -> Windows Files?
How do I convert files created on a Macintosh in MS Office X to files for a Windows XP machine? Does a batch-conversion app. exist? If so, is it reliable and from what site can I find it? Thank you in advance for suggestions and guidance, Mike In article <d51a28d9.0409040651.14e1d9ae@posting.google.com>, mzbpayne@comcast.net wrote: > How do I convert files created on a Macintosh in MS Office X to files > for a Windows XP machine? > > Does a batch-conversion app. exist? If so, is it reliable and from > what site can I find it? Hi Mike! You don't need any special...

Need some info see if money is worth it?
I am on quicken and not happy with it. Does anyone know if money has double entry? Does it it do corporate spin-off and return to capitol? It's not true double entry accounting. Versions higher than the Standard one will do spin-offs, mergers, etc. All have return of capital transactions, but only those that lower cost basis. -- Michael Gordon MVP <anonymous@discussions.microsoft.com> wrote in message news:356d01c49f58$07a615c0$a301280a@phx.gbl... >I am on quicken and not happy with it. Does anyone know if > money has double entry? Does it it do corporate spin-off >...

Having problems opening pst file
When I try to open a pst file - I get the following message "Properties for this informaton service must be defined prior to use" then I get another pop-up "Errors |\ve been detected in the file C:\WINDOWS\APPLICATION DATA\MICROSOFT\OUTLOOK\outlook.pst. Quit all mail-enabled applications, then use the Inbox Repair Tool" Please help!!! I have important contact information I can't get to. GHR 770-451-8565 x11 "GHR" <anonymous@discussions.microsoft.com> wrote in message news:012d01c3ddf8$5ef50650$a301280a@phx.gbl... > When I try to open a...

Locking Cells #6
Is there any way to lock cells in excel so that no body can overwrite it, specially cells containing formulas. TIA Try the instructions here: http://www.officearticles.com/excel/cell_locking_in_microsoft_excel.htm ******************* ~Anne Troy www.OfficeArticles.com www.MyExpertsOnline.com "newbie" <jcd@refid.com> wrote in message news:O0DJJeihFHA.1248@TK2MSFTNGP12.phx.gbl... > Is there any way to lock cells in excel so that no body can overwrite it, > specially cells containing formulas. > TIA > > ...

[HELP] Windows Event log File read???
Hello, I am having a bit a problem in getting information out of the windows system event log file. I know how to get information out of the backup file of the event file. //------------------------------------------------------ OpenBackupEventLog Opens a handle to a backup event log. OpenEventLog Opens a handle to an event log. ------------------------------------------------------// But I do not know how to get informaiton out of the original event file. (C:\WINNT\system32\config\SysEvent.Evt C:\WINNT\system32\config\SecEvent.Evt C:\WINNT\system32\config\AppEvent.Evt) I ha...

Help: pivot chart data in secured Access database wont show fields
Hello Is there a way for me to crate a pivot chart in Excel where the data is in a secured access database. By default (ie using system.mdw) the Access security allows a user view permissions on the tables. When connecting to the database the tables are visible, but the next step where the fields get shown doesnt happen, no fields can be selected. Is this a security issue? Is there a way around this? Thanks A ...

How attach a file in one cell
I'd like to know if it'll be possible attach a file (word, pdf...) in one cell. I'm doing an Excel doc. and I need to put some instructions by doc. attached. in some cells. Thanks! In article <A23394A2-B804-4855-875A-7664B90F4F48@microsoft.com>, How attach a file in the cell <How attach a file in the cell@discussions.microsoft.com> wrote: > I'd like to know if it'll be possible attach a file (word, pdf...) in one > cell. I'm doing an Excel doc. and I need to put some instructions by doc. > attached. in some cells. Thanks! Cells can contain...

Need macro to allow me to go out into another excel file and copy and bring data back..
Hi, I'm looking to have a macro that I've started (below) to allow me to go out to a file that is always in the same location with the same name and copy information from it and bring it back into the file I was originally in. The file from which I want to get the information from is updated automatically every 24 hours from data dumped into it from our MRP system. The macro is to be stored in my "personal.xls" file. The problem is that the original file name is always different. So how can I make the macro come back to this file that has a different name each time to pas...

Form Locking Code Encrypting
Where would I go to find some software that I can use on my DB so as stop somebody copying it. I want to put an Encrypted code on one form that locks all Data on that and 2 other forms will not work unless there is the right code on the main form.................Thanks For any Help.....Bob rofl Access MDB is not secure; you should use SQL Server with Access Data Projects; it's not like someone can copy your database lol On Apr 29, 5:49 pm, "Bob" <x...@xx.xx> wrote: > Where would I go to find some software that I can use on my DB so as stop > somebody copy...