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.
Related
I am writing a small library that all it does is opens a connection with the local SqliteDb present just near the application and reads from the database depending on the search query.
The one using this library does not need the access to Insert or update any record but are updated from lets say me.
SO to create a database i use this:
SqlConnection = new SQLiteConnection("Data Source=Languages.db;Version=3;New=True;Compress=True");
Than to access the database the same but New=False
Now i am using Firefox Sqlite Manager to modify the table such as creating the structure and adding values.
But when i save the file with firefox i get an error on C# stating that it is write/read protected when i try to open the connection!!
Edit: I also tried to use Sqlite Browser: http://sqlitebrowser.org
but then i got that unsupported file format!
Am i missing something?
Regards
The FireFox SQL Lite Manager when open, restricts other applications from making a connection to the SQL Lite database. If you shut down the FireFox Manager, your C# code will execute correctly.
You can refer this answer to understand the nature of the lock applied by Firefox Firefox locks places.sqlite
I have noticed that when creating a table or altering the table somehow from the SQL manager gui, the gui itself creates some headers/meta data that the driver is not able to read and thus it flags it as corrupted.
To solve the issue all i had to do is create the tables and alter as necessary from my application directly,than i can add records using the firefox manager and it seems to be working correctly!
Correct me if im wrong!
I am using SQL server 2008 R2 and VS2010. I made simple application by using this tools. I attach database as .mdf in my application and deploy that application on other machine its works fine. Now if I plan for new release of my app which some extended features, I can upload Code by DLL, But problem is updating .mdf file, to handle this I am exporting database into .xls sheets (Application have one utility to backup database) and then import into SQL Server to create new .mdf file. Someone have better solution on this? Can I open old version of .mdf file in SQL Server(Third party software) and Execute DML/DDL script on it to make latest code and database compatible ? May I keep .sql file in one of my project code and execute it by some utility..? Any Class in C# which can handle this..?
I did not get your query completely. Do you want to upgrade the DB through the application?
You can of course run .sql files through your application, but I'm not sure it would help you change the Database configuration.
Alternatively, if you already have the updated .mdf file and the database name is same, then you can follow the following steps.
1. Detach the database by SSMS in the third party environment through SSMS.
2. Replace the .mdf, .ldf and .ndf (if any) in the disk.
3. Attach the updated .mdf file.
This will get the new Object definitions as well as data.
As far as I'm aware, there is no process for merging .mdf files, because the SQL Server might not be able to identify the similar objects properly as sys tables may be different, and also would not know which data to keep in the final data base, in case the table structure, constraints or data conflicts occur.
However, looking at your requirement, the best way I can suggest is,
1. Generate the Alter scripts for the tables modified (By right clicking on the object name and using Script Table As.. option). Of course, I assume you have the list of objects modified and the modifications.
2. Connect the two DB servers over network and write an SSIS package or Import data from the old DB to the new one for the tables you want.
Hope this helps.
I have a WPF project set up to use a local SQL Server Compact database through an ADO.NET Entity Data Model in Visual Studio Express 2012 for Desktop. The project works great, on first run I can load all of the data, manipulate it as I please and come back later with the changed data still in place.
I noticed while doing a little restructuring to the schema that the data visible to VS was only the very first bits of data that I entered manually when creating the database and the next time I compiled all of the data I had added since was gone!
After some digging, I came to the conclusion that the compiled version of the app was using the SDF file sent to the bin/Debug folder by the file's Content:Copy If Newer build action. This means that there could be as many as 4 different copies of the database to be worried about: project folder, debug folder, release folder, and the deployed copy on the end user's PC.
I would like to have a single copy of the database on my dev machine that is accessed by both debug and release compiled versions and the database explorer in VS that is installed on the end user's PC by ClickOnce. I suppose I could change the connection string to an absolute path during development and hope I can remember to change it back to relative before I publish for deployment.
Finally, I foresee the need to release updates for this application as well and am worried that such an update would erase the end user's data if improperly done. If possible, I would like to be able to only update the schema of the end user's database without touching the data itself whenever I release an update. If this is not possible that is acceptable and I'll just have to make sure I put every structure I can think of into the database before my first deploy.
In summary my questions are the following:
How to share a single sql compact database between VS, debug, and release?
How to handle local database during application deployment and updates, with the optional ability to update the database schema without erasing the data?
I have a similar application and I keep the database file completely separate. Because you may also need to do updates that you don't want the user database overwritten. I have a process that checks the database schema before the EF connection takes place. So when my users install this application it requires they download the database file from my webserver and puts it in a specific location on their computer.
In a C# desktop application, I am using linq to sql to connect to a sql server 2008 r2 database. The application is setup to point to various databases depending upon the value in the app.config file.
The problem is code in the *.designer.cs gets overrriden somehow by defaults in the .net problem framework. When this happens the using system.configuration gets removed from the application and the default settings values are used to get the database connection string values.
When I notice this problem occurs, I need to do the following:
place the using system.configuration code back into *.designer.cs file
change the code so the database connection info is obtained from the app.config file
remove the property settings so the default database connections are not used.
Thus can you tell me what is causing this problem to occur and how to solve the problem?
I have to do this everytime I update the dbml file, its a real pain. There are 2 things you can try.
First - after updating the dbml, right click anywhere in the dbml, go to properities, then under connection string you should be able to see the one from the web.config, or app.config that you want.
If it is not in the list , then set it to none, then go into the designer file and do what you have been doing.
I've written quite a long C# program using Linq to Sql and my data are stored in an MDF file located near my program's EXE.
A part of my program has a form for backing up the database files simply by copying the MDF and LDF files into a user-specified folder.
However if I query the database and then try to replace the original files with the backups, I'll get the file open in another process exception as expected!
The problem is that I don't know how to close the MDF file in the SqlServer instance.
I'm pretty new to Linq to Sql and I let the Visual Studio's wizards to handle most of the job! So I'm sorry in advance if anything I'm trying to do sounds stupid! :D
Any help or suggestion for better programming methods for my case is greatly appreciated.
This may not work for your needs, but you might want to consider switching your application to use Sql Server Compact Edition ("SqlCE") instead of Sql Server (proper). It sounds like you're using the database as essentially a backing file for your application, which isn't really the intended purpose of Sql Server. SqlCE is designed specifically for this kind of thing, and works great on the desktop. You can easily close your connection to SqlCE and manipulate the SDF file like any other file (what you're trying to do with the Sql Server MDF file, unsuccessfully).
Update: This looks like what you need:
USE master
GO
ALTER DATABASE YourDatabaseName
SET OFFLINE WITH ROLLBACK IMMEDIATE
GO
(from this answer)
You need to stop the SQL server or take the database offline before copying the files.