Backup SQL Server CE database with C# - Import-Export - c#

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

Related

Release plan for my application on other machine by using .mdf file

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.

What database to use if I need to deploy an app with one small table?

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.

How to backup SQL Server database (mdf) programmatically to later import it rowwise?

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;

How to Backup Automatically Mysql in C# coding?

I want to backup my Database in MySQL for Each time Application is Closing.
I googled for the DUMP in MySQL. but i got only php or batch file programs.
Any one know how to Backup Automatically Mysql in C# coding?.
Or Executing Batch is the Only way. Is it possible in Winform C#?.
My Connection String is -
connectionString="server=192.168.1.100;User Id=root;database=mcs_sps;Persist Security Info=True"
Thanks in Advance!.
There are good samples and a good best practice in the following link: http://forums.mysql.com/read.php?47,214274,214274#msg-214274
I think your best shot is that aL3891 suggests unless you do not want to pay . If you want to pay for this simple task check out these links:
http://www.devart.com/dotconnect/mysql/docs/Devart.Data.MySql~Devart.Data.MySql.MySqlDump.html
http://www.devart.com/
Using the Process class you can start an instance of Mysqldump or Mysqlhotcopy, command-line programs for dumping an entire mysql database.
The best solution is this http://www.codeproject.com/Articles/256466/MySqlBackup-NET-MySQL-Backup-Solution-for-Csharp-V
I tried it in my application and it works perfectly

Replace a database MDF file with backup file during runtime in C#

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.

Categories