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.
Related
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 file that contains a SQL Server 2012 database backup. I am using this file with a C# WinForms application, that creates a database, restores the backup into it, and then performs some more functions.
I don't want to just add this backup file to my application, because this is a security issue. I was thinking I should add the backup as an embedded resource, and then save it to a temp file, restore from it and delete it.
Is there a better way to do this? Is there a way to access an embedded resource directly through a file path that I can pass to SQL server to use in the RESTORE DATABASE command?
SQL Server has no idea whatsoever about how to restore a database from something living inside an executable. Given that you state that you've considered it be able to be an embedded resource, I am going to assume that it's not a "last known good" backup, but more a "default" backup.
If I am correct in this thinking, then you're basically saying you have a default set-up. If that is the case, you don't need a backup at all, your code just needs to be able to create a database and populate it with certain things to "become" that default state. By embedding a backup as a resource, you are (in my mind) stating that the structure and data is well known, so why go down the RESTORE DATABASE path at all?
I have created a C# windows application with vs2010 and I'm using a SQL Server CE database. I'm looking for a way to backup my database programmatically.
Is there a way to export/import my entire database (as a .sdf file) or just copy it to another location and then import it and replace the current one?Could anyone provide me the code in order to do this?
I'm relatively new to this but I'm guessing this is not something as difficult as it sounds. I couldn't find a clear answer anywhere so any help would be appreciated!
File.Copy will do the job, I believe. You can get the full path from the SqlCeConnection.Database property
I need to create a very simple app that has one table that will need to store records over time. What is the best way to do this with vs 2010 if i do not want to install a database engine on the end users machine.
I would like to just package up the database file with the exe when deploying. There will only be one user accesses the table but i do not want to have to install sql express on the end users computer.
Any suggestions? Thanks!
You might be looking for SQL Server Compact Edition - if it is just one table and not very much data you might consider just using an XML file as well though.
Some suggestions:
(typed) DataSet using Write() and Load() to save the data to an XML
file (very convenient with Visual Studio)
SQLite
Object serialization
Try either SQL Server Compact or in another scenario which I dont like MS Access.
http://en.wikipedia.org/wiki/SQL_Server_Compact
http://en.wikipedia.org/wiki/Microsoft_Access
Some options for embedded relational database systems:
SQL Server Compact Edition
SQLite
Firebird
For a more complete overview see http://en.wikipedia.org/wiki/Embedded_database
if you want to save data very simple,you may Serialize data to XML file, or if you want to save as smaller.
Add Dataset, when you start program load it to memory, when you close program you may save it as xml file.
My aim is to backup a database (.mdf) as one file with my web application project written in C#.
The backup should later on be loaded on a "restore"-page, where the data in the backed-up tables could be appended to the original database row by row.
What would be a good practice to implement this?
I thought of just copying the mdf file, but then I read about attaching and detaching of the database. Furthermore I don't know what to do with the _log.ldf file.
I'm looking forward to your hints. Thank you in advance for your help!
EDIT: I can only use the free SQL Server Express for this, because I want to distribute my program to other people.
Probably, you refer to the Backup and Restore using C# for Sql Server to get a complete idea about writting a code in C#, which has helped me a lot when I was using it.
By the use of Backup class in C#, you can get all the facilities to backup as well as restore.
If you are only interested in appending the data rowwise afterwards, perhaps it is easier to export each table to CSV and import it afterwards (so you have rowwwise control in C#).
If you insist ine one file, just add all the CSV's to a zip.
You can use the FileHelpers library for this (http://www.filehelpers.com/) and you will have it up&running in no time.
Apparently there is a Backup class in the SQL Server Management Objects library.
You might want to check that out first, as it doesn't look overly complicated:
http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.backup.aspx
It would require you to have two or three libraries installed on the server you run it on, though they are fairly small - not Windows SDK sized...
pls go through this link on how to backup data and restore using c# and sql server .In addition to you have to add these names spaces
Microsoft.SqlServer.Management.Smo;
Microsoft.SqlServer.Management.Common;