Visual Studio and an Access Database - Operation must use an updateable query - visual-studio

I am deploying an .accdb file to the same directory as my windows forms program is being installed. However when attempting to update data in the database i get a "Operation must use an updateable query" error.
The file attributes are not read only, however when I open the accdb file it stats that the database has been opened read only. I must save a copy to make it not read only.
I do not believe this has anything to do with permissions, or file attributes. This is only on Vista, XP appears to work ok. The original accdb file is not read only when i open it.
What is visual studio doing to the accdb file to make it read only? and how can i fix it.

Are you installing into the Program Files heierarchy? If so that's read only for users and would cause the Access database to be read only. You should be installed any Access database file to the users Application Data folder.

Related

Visual Studio reading Access database

I have written a Windows application in Visual Studio that reads data from a Microsoft Access database. Everything works fine; however, after publishing the application I need it to read the database when the database is changed or updated. Can anyone help with where to put the Access database and how to get the application to look at it when it opens?
It sounds to me like you can place the Microsoft Access (presumably, a single .accdb file) anywhere you want to put it and then--as long as your program knows where that file is--simply use System.IO.FileSystemWatcher.
See this question/answer(s) for more details:
Notification when a file changes?

SQL Server 2008 Database Changing Read Only state to 'True' After Deployment

I'm creating a Installation Setup for one of my applications using visual studio setup project in VS2010. In which I'm accessing my database from application folder. I have added both MDF and LDF files to Data folder in Application folder.
Everything works fine and I'm able to install the application, but when opening the application I'm getting an error saying 'Cannot update database because its read-only'. I have checked the database with SSMS and see that the Read-only flag is set to True by default and database is showing in gray color. After change the read only property the application works fine.I have done a lot of searching and rebuild-ed the project many times with changing the database. How can i get rid of this issue ?
A couple of possibilities:
In your setup project, on the properties window of the DB file in the File System on Target Machine view, make sure you didn't accidentally set the Readonly property to true.
If the database is in your Application folder in (for example) the Program Files folder, well that location is read-only to limited users. You haven't said whether your app is for all users or just administrators. If it's for a limited user then install the file to User's Personal folder or a location that limited users can write to. If the app requires admin privilege then your app needs an elevation manifest to cause it to run with admin privilege, if that's why the DB is readonly. Running your app as administrator would be a quick test to see if this is the issue.

VS2010 Code First MVC3 SQL default Logfile.ldf name format ignored, creates second logfile_log.ldf file

Using VS2010 code first and SQL Server 2008r2 with an MVC3 webapp, the SetInitializer creates a db as expected when missing or on model changes.
The db created has the filename format database.mdf and the **logfile database.ldf (can this default "name.ldf" format be changed to "name_log.ldf"?).
When opening the database with VS2010 server explorer (double-clicking the db in solution explorer), the VS2010 creates a new second logfile formatted database_log.ldf.
I can now browse the database in VS2010 server explorer, but can no longer debug, dropcreateonmodelchange or otherwise use the db.
When trying to debug error shown is:
{"One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. If this is an existing database, the file may be corrupted and should be restored from a backup.\r\nCannot open database \"database\" requested by the login. The login failed.\r\nLogin failed for user 'My-PC\Me'.\r\nLog file 'C:\Users\Me\project\App_Data\database.ldf' does not match the primary file. It may be from a different database or the log may have been rebuilt previously."}
...which is fair enough since the project is expecting the current log file to be "database.ldf".
The ASPNETDB.MDF and aspnetdb_log.ldf files/names are also created and don't have the default log file name issue, so the issue doesn't apply to ASPNETDB.
Is it possible to change the default log file name for the SetInitializer to create a "database_log.ldf" formatted log file name instead of the "database.ldf"? I've googled without success, and looked at all the settings and options I could find in VS2010. Is the format stored in a config file somewhere that can be hand edited?
To continue using the database in VS2010, I've ended up deleting the database.ldf log file, and renaming the new database_log.ldf file to the original database.ldf name.
It's not elegant or correct, but it works for development.
This doesn't address the incorrect behaviour of codefirst creating one version of the log file and server explorer creating a second differently named logfile at different stages of the development process within the same VS2010 environment.
I've got exactly the same problem using VS2010 Express and SServer 2012 Express. I'm trying to work through Julie Lerman's book Programming Entity Framework 2nd Edition.
Is there no better solution than this - renaming the logfiles? I've tried to rename the files, but Windows will tell me that the file is in use, even when I shutdown both VS and SSMS. A Process Explorer search for the DB tells me that the file is in use by SServer.
I've found that all I have to do is try to create a connection string using the DB Connection wizard, and the DB in SServer gets the "extra" logfile created, and I get the same message you are seeing.
Is this a bug in VS2010Ex? Are there patches available? How do we prevent VS2010 from corrupting the DB?
Here is the directory after creating the DB, but before trying anything from VS:
E:\Program Files (x86)\Microsoft SQL Server\MSSQL11.SAMPLES\MSSQL\DATA>dir programmingefdb2*
Volume in drive E is JET20110708-1
Volume Serial Number is EA22-1F7C
Directory of E:\Program Files (x86)\Microsoft SQL Server\MSSQL11.SAMPLES\MSSQL\DATA
08/09/2013 11:39 AM 10,485,760 PROGRAMMINGEFDB2_Data.mdf
08/09/2013 11:39 AM 1,310,720 PROGRAMMINGEFDB2_Log.ldf
2 File(s) 11,796,480 bytes
0 Dir(s) 250,238,443,520 bytes free
Here is the directory after making the connection in the VS project but without even trying to run the update code (and without copying the DB into the project):
E:\Program Files (x86)\Microsoft SQL Server\MSSQL11.SAMPLES\MSSQL\DATA>dir programmingefdb2*
Volume in drive E is JET20110708-1
Volume Serial Number is EA22-1F7C
Directory of E:\Program Files (x86)\Microsoft SQL Server\MSSQL11.SAMPLES\MSSQL\DATA
08/09/2013 11:51 AM 10,485,760 PROGRAMMINGEFDB2_Data.mdf
08/09/2013 11:51 AM 516,096 PROGRAMMINGEFDB2_Data_log.ldf
08/09/2013 11:39 AM 1,310,720 PROGRAMMINGEFDB2_Log.ldf
3 File(s) 12,312,576 bytes
0 Dir(s) 250,237,927,424 bytes free
To anyone reading this thread and especially to the author, I found the solution over on the MS SQL Server forum. Here is the answer:
Visual Studio corrupting SQL Server logfile

Moving a .sdf file into isolated storage

How do I move a .sdf file into my isolated storage and after I have moved it is there a way to delete it as it is of no use. I have added my .sdf file as a content in my project.
Your question is not very clear, but let me see if I get this. You created a database, added it to your file as content to your project so that you can have all the data present when the user installs your app. Then you are copying the data from the read-only .sdf file into a database that you are creating on first run, so that you can read/write to it. Correct?
If so, I do not believe there is a way to delete the read-only file that you included with the install.
If your database is large enough that you are concerned about the space it will take by having two copies of it on the phone, I would suggest placing your data on a server, creating a web service, and access that web service on first run. Place a notice on the screen that lets your user know that it is downloading information that will only be downloaded once, and that subsequent launches will not take as long. Be sure you include code to prevent a problem should the download be interrupted by a phone call, text message, back key press, start button, or other event. Make it be able to continue the download if it was interrupted in a prior run.
To answer your question, .SDF is a format of Microsoft SQL Server Compact (SQL CE) databases. The link you have pasted talks about SQLite databases.
This the way to download the entire Isolated Storage onto your device.
Open cmd and go to the following directory
C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Tools\IsolatedStorageExplorerTool
then use the isetool.exe to download the Isolated Storage along with the .sdf file onto your machine.
isetool.exe ts xd [Product_id_here_see_WMAppManifest.xml] "D:\Sandbox"
You should get message like download successful into D:\Sandbox.
You can also upload the sdf by changing the argument ts with rs

cannot save record in attached .mdf file with EF 4.0

I want to create a VS 2010 project with EF 4.0 model.
But I have no records and errors after inserting new record with attached .mdf file .
But if the base is connected to Sql Server, it works fine.
Please help me to insert records to attached .mdf db
You need to make sure DB is not in a shared folder, is on your pc, is not read only, that you have write permissions to the folder that contains it and there is no DB already attached to service with same name. Make sure you can connect with Server Explorer window first.
Consider using SQL Server CE, if you want to work with loose DB files.
Also, make sure the file is not overwritten with each build. (Use "Copy if newer", not "Copy always".)

Resources