I'm trying to prevent the following from coming up when reading an Excel file using ADO.NET:
The Microsoft Jet database engine cannot open the file ''. It is already opened exclusively by another user, or you need permission to view its data.
I know it is happening because another user has the file open, but I can't get rid of it. I've tried adding Read Only=True; to my connection string, but that doesn't work. I've also tried to set the Mode of the connection to read-only, but I can't figure out how to in C#.
Here is my connection string:
Provider=Microsoft.Jet.OLEDB.4.0; data source=test.xls; Extended Properties="Excel 8.0;HDR=No;IMEX=1"
Thanks for the help!
This can also come up if you don't have permissions for the file, not just when it's exclusively open. In any event, I always found the best workaround for this problem was to automate a copy of the file to a new temp file. Copies are almost instantaneous, even for large files, so they shouldn't slow you down much, and since your application is only read-only, you don't have to worry about discarding the temp file afterwards.
Furthermore, this will clear up any permissions issues that can be cleared up. If you have read permissions, the copy will work, if not, then there's no workaround anyways. You will have full permissions for the copied file if you make the destination path of the temp copy the same path as the workbook. (Application.Path)
Use the FileCopy srcFile, destName command for this.
Well, you said you tried changing the mode to read only, how exactly did you do that?
As per MSDN, by default, the OLE DB Provider for Microsoft Jet opens Microsoft Jet databases in read/write mode. To open a database in read-only mode, set the Mode property on the ADO Connection object to adModeRead.
So in C# it should have looked like:
ADODB.Connection myConn = new new ADODB.Connection();
myConn.Mode = adModeRead; //1
myConn.Open(strConectionString, null, null, -1);
Related
I wanna improve one application which database is unknown for me. So i have interface to work with but no data to fill forms/documents etc in a new application... Installation folder only containts .dll files without any clue of database. For now i have tried to inspect .dll files with JetBrains and found only source code of those dll's without any "physical" location of database on my hard drive so i started to think it's embedded in application. I also inspected all my hard drive to try find it in some hidden folder without success. After further investigation i figured out that i have to get Microsoft Access database who works with Jet Oledb 4.0.. I found this line of code where it says :
this._connectionString = "PROVIDER=microsoft.jet.oledb.4.0; Data Source=" + this.DataBasePath + "; Jet OLEDB:Database Password = 12345678";
So basically i know the password of that database but i don't know where is that database. And now i am stuck because i have never worked with embedded database. I only got installed application and installation file (which was generated thru visual studio).
Is it possible to get tables/data from embedded database and if so how to do it? Is it maybe possible to get database from installation file and if so how to do it?
Any help would be nice.
Thanks KuKeC
Unless I'm wrong there has to be an actual file to store the database so why dont you just look for a *.db file in your system and open it with a db tool to check the content
A little background first: I need to extract a database from an SQLite dll file for my client who has burned bridges with the previous dev.
I have these 4 files sitting on my desktop:
System.data.SQLite.Linq.dll
System.data.SQLite.dll
SQLite.Interop.dll
Payroll.exe
I am also given the password for the SQLite DB which Payroll.exe uses to interact with the database. With this is it possible for me to dump an SQL file (so I can work with something familiar)?
Major problem is that I have no experience with C#, .NET stuff and DLL files (I know it stands for Dynamic Link Library!)
I'm willing to dabble and learn some C# to do this.
As suggested, I decompiled the source and found some interesting code:
namespace Payroll
{
internal class Payroll_Database
{
private static string ConnectionStr = "Data Source=.\\System.Data.SQLite.Linq.dll; Password=******!";
private static SQLiteConnection Connection = new SQLiteConnection();
It seems certain that System.Data.SQLite.Linq.dll is the database file. I have trouble opening it with tools such as SQLite Browser. I have tried renaming it to a .db, but again no dice.
Your database won't be in the dll and exe files you list.
It should be in a separate file, possibly with an extension like .sqlite, .sqlite3, .sdb3 .db or .db3 (but this is not mandated).
If you can locate the db file, you can open it with any sqlite client (e.g. you can use the firefox sqlite-manager plugin), and extract the data as required.
You could probably find the database filename from the Payroll.exe source code, which you can read by opening Payroll.exe in ILSpy.
If you cannot locate the db file, you will be unable to retrieve the data. In which case, the best you can achieve with the files you list would be to determine the database schema by reading the payroll application's source code.
Update:
Now you've found the connection string, it looks possible that the database could indeed be the System.Data.SQLite.Linq.dll file, and the developer has given it a name to match a known .NET assembly as some kind of security through obscurity.
If that is indeed the db file you should be able to open it directly in a SQLite client application like the firefox sqlite-manager plugin I mentioned above. If you can't open it in a SQLite client application, then you can to check to see if it is a .NET assembly after all by opening it with ILSpy or Microsoft's MSIL disassmebler.
Update 2:
According to this SO question, you should be able to open your encrypted db file with the free SQLite2009 Pro Enterprise Manager.
I have a weird problem with my local database. I added it as a .mdf file, and created 2 tables in it.
I tested the connection and the message box states that the connection is working properly.
The problem appears when I try to insert data into that database from my C# application. I tried 2 different ways:
By using SqlCommand.ExecuteNonQuery("INSERT INTO ....")
By using .dbml file (Linq-to-SQL) datacontext
When I try to insert data, no error is thrown, but no data is inserted into the database either. What could be the problem?
I'm using C# in VS 2010 and SQL Server 2005 to make my application
most perceived problems with MDF files tend to boil down to one of:
what file have I actually opened?
is my build/run process actually copying over that file every time I run the application?
do I have gratuitous error-handling that is swallowing an exception?
Check your connection string, and look in the execution folder (not the project folder), until you are very sure which file is being opened. It should get updated after your work (make sure you dispose the connection etc properly).
Also: try fetching the data in a new data-context immediately after the insert, so see if it made it in or not.
I have a database that I connect as a file, i.e. through the AttachDBFile attribute in ConnectionString in web.config file of ASP.NET site. The database is in the App_Data folder od the website. I made a small app, that should allow to copy paste (a kind of backup functionality) the database to any chosen directory. For this I am using FileDialog box for allowing the user to chose the destination directory. I am using the .NET FileSystem API for copy pasting. The problem is that, I cannot copy paste the database till I shut down the SQL Express service. During copy, a dialog saying that the file is currently under use by other process is shown. If I turn off the service I can copy paste the database.
I used AttachDBFile attribute, since I thought that it will allow such copy, since this doesnt directly attach the database to the server. But now I think its not like that. :(
So how I can deal with this. Please help. Thank you.
You get the error since the file is locked while it's in use.
Have a look at the recommended ways to backup your database at msdn: http://msdn.microsoft.com/en-us/library/ms187510.aspx.
I am trying to learn Monotouch and have an SQLite db with information.
Using the examples here I can open, fill and query the database (which is set as "Content" in my project), but when I rerun the program, no data exists in the database again.
I am guessing that somehow I have to save the database file to the Documents directory after changing it, but I can't quite figure out how to do that. Do I have to open the file with File.Open, then open another one in Documents and write to that one line by line? That doesn't seem efficient or correct.
Does anyone have any examples of this?
What is the "Copy to Output Directory" set to for you db file? I'd guess the most likely problem is that every time you build in the IDE, the blank db is overwriting the modified db from your last session.