How can I open a locked mdb file? - c#

I have a database that is locking mdb's and such that I'd like to backup. However the tool (I have the source) I am using opens the file before backing it up and finds that it is locked.
Is there a way I can open it for read-only purposes?
For reference the tool uses C# and .NET 2.0 (but can be updated to 3.5).

The reason your tool locks the file is to prevent changes to the file as it is being backed up. For example, if you begun your backup, but halfway in the DBMS (i.e. SQL Server) decided to make a change to a file, then your backup would be corrupt.
I recommend you use the tools that are provided with your database solutions to perform a backup. The other option is to stop the database before backing it up.

If the DBMS is holding a write lock on the file, and you read it, you're risking the DBMS writing the file as you're reading it. Depending on what part was written, you could end up with a corrupt backup of the file. You're best off reading the file only if the DBMS isn't writing to the file or letting the DBMS handle its own backups.

This is similar to this question:
Opening a file's Shadow Copy if the current copy is in use
It depends on how the database is opening the MDB file. If it's not allowing read sharing then you're out of luck unless you are able to open the shadow copy. There's a discussion on how to do this here:
How do I copy a file or folder that is locked under windows programmatically?

Related

Rollback the PDF generation in C#

I have written a C# function for
1) Generating PDF file into folder in its own solution.
2) making entry into SQL database with PDF file name & its size.
I want to,
=> Rollback the PDF generation if the Data insertion into the Database fails.
Because the File may not be used in future.
i.e. If someone stops the Process in the middle before the entry made into Database then for that situation I have to use precaution like this.
Because the File may not be used in future.
Is it possible to rollback the Generation of PDF?
The simple solution is to delete the generate Pdf file when the database update fails. Deleting a file is fairly easy File.Delete(fileName);
You can use transactional file manager library given by codeplex along with transactionscope to achieve your task. Here is the link as to how you can implement transactional file manager along with database transaction.
Also the newer versions of windows has something called TxF (Transactional NTFS) which you can use. Check out the link here. Not very sure if this will address your problem but you can definitely try.
Hope this helps.

How to write Microsoft.Office.Interop.Excel.Application object on asp.net output stream

I was using the Microsoft.Office.Interop.Excel in C# to create a custom .xlsx file.
In doing so I created a Workbook object. Due to the nature of complex SQL queries to grab the data, process it, and apply via Interop the custom styles and formatting the code is very lengthy. Not to mention the very careful process of avoiding memory leaks from the Interop itself, and ensuring that Excel actually closes properly after running.
I originally was testing it out as a console application, and got it working to my satisfaction. What it does is save the end result to the filesystem using the SaveAs member.
However, my next goal was to instead redirect the output as an output stream to asp.net similar to this question here. I've done some rudimentary research and I cannot seem to find an approach that does not involve first saving the Workbook to the server's file system. This may cause conflicts if several users are accessing at the same time, etc.
So my question is, is there an easy way to set the asp.net ContentType for .xlsx and stream out the Workbook object without saving it to the file system? If not, is there a way asp.net can save temporary files automatically without conflicts, serve the temp file, and then delete the temp file after it's been served?
I agree with the comments that you should avoid using Excel Interop server-side, and the third party libraries I've used (EPPlus, Aspose) all support streaming the output. However, if you want to save temporary files without conflict you can use Path.GetTempFileName.
If your ASP.NET app is running under an account without a profile, you may need to give it write access to %WINDIR%\Temp or whatever temporary directory it uses.

Detect and handle application sudden endtask ,restart and shutdown?

I have an application that stores an XML Encrypted Document, each time the application wants to access the XML file it have to (decrypt-->read-->write-->encrypt). if a sudden shutdown or restart even a end task from task manager will result on either corrupted data in the XML or a un-encrypted XML file, so in the next run it will produce an error.
What i though about is to store the whole content of the XML in a the database and check if there are any errors then replace the old content.
Any ideas, tips, or thoughts much appreciated.
You should do the decryption/encryption in memory. Never, ever store unencrypted data on disk. Load the encrypted data, decrypt it in memory. When saving, first encrypt in memory, then store.
Replacing the entire content of a file in a way that is safe even in the event of sudden shutdowns is hard. A workaround is to create a new file under a temporary name and when it is written completely to disk, delete the old file and rename the new one. There are other implications of this though, such as security rights specific to the file being lost.
A better, but more advanced option, is to move everything into a database with transaction support.

Creating File On Full Disk

Is it possible to create a file on a disk which is full??
Does creation of the file take any space??
Basically I am seeing a case where C# has created but failed to write anything whhich I think points to a full disk.
Does anyone know whether creating a file on a full disk will fail or not??
This wa done using c# o Windw xSerevr- The log file was also written to the same drive
Creating (empty) files should still be possible in most cases. The MFT is a separate part of the volume which won't get used for file data.
It should even be possible to store small amounts of data without needing more than the file entry in the MFT. NTFS can store streams as "resident data" in the stream descriptor which doesn't need any additional space, but only works for very small files.
I think your issue is another problem, though. It may be that you have permissions to create a file but not to write anything to it. You might want to check the ACLs of the location where you're trying to write.

Copy a changing file with File.Copy

I'm writing some sort of backup tool that has to copy all the files in a directory. Now I'm using C#'s File.Copy(String, String, Boolean) method. But another application (which I can't change) simultaneously writes to the files in that directory.
So now I wonder if it is possible that a file gets changed halfway the copying process creating a corrupt backup file?
Thanks
No, it is not. Underlying file system will make sure this is not happening.

Categories

Resources