ESENT database engine file access denied - c#

I am trying to using the ESENT windows database with the managedesent library but I always get the error
Error FileAccessDenied (JET_errFileAccessDenied, Cannot access file, the file is locked or in use)
The code to open the database is
m_Dictionary = new PersistentDictionary<string, PropertyStruct>("BaseEstateCachedPropertySummary2");
I am testing this on Windows 7 and the application is an ASP.NET application. Does anyone have any ideas about this, documentation is pretty slim.

Are you trying to open an existing database used by another service (e.g. the Windows Update database)? Esent databases are single-process only so you have to stop any other processes that are using the same database.

I found an answer. Apparently I didn't have write permissions to the default directory where it puts the database so I had to specify the full path such as.
m_Dictionary = new PersistentDictionary<string, PropertyStruct>(#"c:\Data\BaseEstateCachedPropertySummary2");

Related

C# Executing a query and getting data out of an Access Database (accdb) without using ACE.OLEDB

I'm writing a WPF application.
Trying to use the normal method of getting a connection returns an error similar to: "The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine."
ACE.OLEDB has never been installed on this machine so this error makes sense.
I'm trying to create this application in a way so that our users won't need to contact IT to have the application installed. Getting IT involved is a no go situation and the project will be abandoned.
Another team has an Access database (accdb) that I want my application to extract information (only read, no insert or update). I talked to the team and they won't convert this database back to an earlier version (mdb).
After my research I assume that installing ACE.OLEDB without using Admin privileges is impossible. Because of this and my application requirement of not requiring admin privileges I need to start looking for "Mutant"/Dirty solutions that don't involve ACE.OLEDB.
I tried using power-shell but I'm getting the same problems as I had with C# (requires IT to install ACE.OLEDB).
I have two potential solutions. One write a VBA script that opens up the database and dumps a query result into a file. My C# application would call this VB script and then parse the created file.
The second option is to create a new Access process using Process.Start(fullFilePath) and somehow pass the command to execute a query and somehow pass the results back to the executing application (either via a method return or first to a file).
How would you get the data out?
Is there a way for C# to duplicate the DB file and convert it from (accdb -> mdb)?
This is the second question I ask that is very similar.
C# Connecting to Access DB with no install
The difference between the two (to prevent this is a duplicate question) is that in the previous question I was looking for ways to install ACE.OLEDB without admin privileges while here I'm just looking for any other work around.
Found a workaround. It uses Microsoft.Office.Interop.Access found in NuGet.
var accApp = new Microsoft.Office.Interop.Access.Application();
accApp.OpenCurrentDatabase(#tests.DatabasePath);
Microsoft.Office.Interop.Access.Dao.Database cdb = accApp.CurrentDb();
Microsoft.Office.Interop.Access.Dao.Recordset rst =
cdb.OpenRecordset(
"SELECT * FROM Users",
Microsoft.Office.Interop.Access.Dao.RecordsetTypeEnum.dbOpenSnapshot);
while (!rst.EOF)
{
Console.WriteLine(rst.Fields["username"].Value);
rst.MoveNext();
}
rst.Close();
accApp.CloseCurrentDatabase();
accApp.Quit();

Create and access a sqlite-DB on Azurestorage via storage connection string from different Application servers

I have a Webapp for different customers-DBs which runs on several Application Servers. The customers are each assigned to an instance on a AS.
For several of the customers, certain data is saved in additional SQLite-DBs on the Application Servers themselves; when this kind of data is added, the WebApp tests whether the according SQLite-DB already exists on this AS and if not, it creates it by using the following code:
dbFileName = "C:\\" + dbFileName;
SQLiteConnection.CreateFile(dbFileName);
using (System.Data.SQLite.SQLiteConnection con = new System.Data.SQLite.SQLiteConnection("data source=" + dbFileName))
{
using (System.Data.SQLite.SQLiteCommand com = new System.Data.SQLite.SQLiteCommand(con))
{
con.Open();
The problem is that if I assign the customer to an instance on another AS, the SQLite-db has to be created again, since it can't access the one on the other AS.
Now my idea was to create the SQLite-dbs on some azure storage, where I could access it from every AS, but so far I'm not able to access it via a SQLite-Connection.
I have knowledge of my specific SAS (=Shared Access Signature) and Connectionstrings like the ones specified on https://www.connectionstrings.com/windows-azure/
but I'm not sure which part I should use for the SQLiteConnection.
Is it even possible?
The only examples on connections to Azurestorage that I found so far are via HttpRequests (like How to access Azure blob using SAS in C#) which doesn't help me or can anybody show me a way to use this for my problem?
Please tell me if you need more information, I'm kind of bad at explaining things and not taking into account that many things aren't common knowledge...
You can not use Azure storage blob as a normal file system. The data source in SQLite connection string should be a file path or memory.
If you want to use Azure storage blob, as far as I know, you can only mount Blob storage as a file system with blobfuse on Linux OS. But this is not 100% compatible with normal file systems.
Another choice is to use Azure storage file, it supports SMB protocol. You can mount a network driver and use it.

Server.MapPath Access is denied On Azure

I am exporting an excel using c# code and spreadsheetlight dll.
I am trying to insert a picture into excel, everything works well on local machine but on live web site it gives following error
Access to the path 'F:\sitesroot\0\Images\Logo.png' is denied
My c# code.
string filePath = Server.MapPath("~/Images/Logo.png");
SLPicture pic = new SLPicture(filePath); //SLPicture - An inbuilt class for inserting image
Currently what I am doing is manually providing access rights on IIS server but which is not the correct way to go since each time I upload the service I have to redo that again and again
Any expert advice - How I can provide access right through code?
Or How to get rid of this error.
Thanks.
Assuming you are using Cloud services...
You are supposed to use Local Storage for this (http://msdn.microsoft.com/en-us/library/windowsazure/ee758708.aspx).
Basically;
In your Cloud project, go to the properties for the Role and select Local Storage.
Add a new local storage and give it a name, i.e. "foo" and a maximum size. If this is for temporary processing rather than caching, you probably want to tick "Clean on role recycle".
In your code call RoleEnvironment.GetLocalResource("foo") to get the folder name you can use and then do something like Path.Combine("path","Logo.png") to get a file name which you can then write to.

Save SQLite database into Documents Folder win Windows Store App

I've been writing a Windows 8 Store App using XAML and C# and the target device is a Windows Surface tablet.
Within my application I use a SQLite database.
To create/load the database I use the following code:
DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "MyDatabase.sqlite");
However I'd like to store the database in the Documents folder so I tried the follow line of code to replace my line above :
DBPath = Path.Combine(Windows.Storage.KnownFolders.DocumentsLibrary.Path, "MyDatabase.sqlite");
When I try this I get the following error:
WinRT information: Access to the specified location (DocumentsLibrary) requires a capability to be declared in the manifest.
So I open Package.appxmanifest and select Documents Library under capabilities and under the declarations tab I've added a File Type Association. Then under Properties I've entered
sqlite in the Name box and .sqlite in the File type box. I've entered no other information on the Package.appxmanifest screen.
Now when I run the app I receive the following error:
Could not open database file: MyDatabase.sqlite (CannotOpen)
When I inspect Windows.Storage.KnownFolders.DocumentsLibrary.Path it equals "". I would have expected the full location path here.
Can anyone help and has anybody ever saved a SQLite database to the documents folder?
Thanks in advance.
KnownFolders.DocumentsLibrary does not have a path since it's a virtual location. Take a look at this answer for more details. This could make it impossible to open a SQLite database there, at least using the APIs available in sqlite-net library.
Also before using access to Documents library in your Windows Store app keep in mind that for publishing an app with this capability to the store you need to have a company account. With an individual account such applications will be rejected.

C# Application can't read/write to files created by administrator when run in limited user account XP

I have an application that is useable by all users (admin or limited) in .NET (C# specifically).
When the application first launches - it creates a few files that it needs in the C:\Documents and Settings\All Users\Documents\ for all subsequent launches.
If the limited user in XP is the FIRST user to launch the application it creates the files fine and both the limited user and administrators can run fine.
However if the Administrator (or I am guessing a different limited user) is the first to launch the application then the limited user is NOT able to run the application.
The two files that it is NOT able to read/write to if created by an Administrator is a Log4Net log file and a SQLite db file.
The SQLite database file is being created with a straitforward .NET File.Copy(sourcepath, destinationpath). The sourcepath is a seed database file installed with the application - so on first run it copies that from the C:\Program Files\app install\seed.db
Is there a way to set the permissions on the file when I copy it? File.SetAccessControl() perhaps? I am not clear on how that works.
The other issue is that the log4Net rolling file appender will not roll the old file and create a new as the old file was created by the admin user when they ran the app.
Any ideas? Ironically this all works perfectly fine in Vista with limited/admin accounts - this is ONLY happening in XP with admin/limited accounts.
I think SetAccessControl is the way to go. Maybe something like this:
// get the existing access controls
FileSecurity fs = File.GetAccessControl(yourFilename);
// add the new rule to the existing settings
fs.AddAccessRule(new FileSystemAccessRule(
#"DOMAIN\Users", // or "BUILTIN\Users", "COMPUTER\AccountName" etc
FileSystemRights.Modify,
AccessControlType.Allow));
// set the updated access controls
File.SetAccessControl(yourFilename, fs);
Note: It's important that you get the existing access control list from the file and then add your new rule to that. If you just create a new access control list from scratch then it will overwrite the existing permissions completely.
Yeah, it's the SetAccessControl method all right, there is a good example here
(the post from satankidneypie)
Good luck

Categories