C# SQL Server Express backup and restore - c#

Basically I have a windows form C# program that uses a SQL Server Express database to store data entered by the users. I want to add the ability for the user to be able to backup their database and then load a backup or another database into the programs database.
If someone could point me in the right direction like an article or tutorial that would be great, because I have hit a dead end.
Thanks

You have a few options to do this:
On the server directly call BACKUP DATABASE, RESTORE DATABASE commands.
Use SMO (SQL Server Management Objects) to perform the backup restore operations programatically. (Specifically this sample)
Use some third party utility/library.

You may want to look at using this suggestion:
http://codeasp.net/articles/sql-server/118/backup-your-database-using-an-sql-command-export-to-bak

The nice thing about SQL Server is that you can do more or less everything with SQL (T-SQL or some variation thereof) - this is nice because it means that it is relatively easy to do most things without having to worry about (for example) the availability of SMO and further in a reasonably generic fashion.
The second nice thing is that if you run a command in SQL Server Management Studio (a version of which can be downloaded for SQL Server Express) it will, more often than not (and certainly in the case of Backup and Restore), offer to let you save the the script to a file instead of executing it.
So, it would be straightforward for you to use SQL Server Management Studio to determine the structure of SQL based backu and resotre commands and from there to integrate those into your application.
There is, however, a "gotcha" - SQL server will only save backups to/restore backups from a drive that is visible to the machine & service account upon which the SQL Server instance is running, almost certainly not an issue if your application is running on the same machine as the user but potentially a problem if they are not.

Related

detach an SQL server from visual studio

I'm pretty new to developing in Visual studio and working on databases.
I am working on a program that deals with reading and writing data to a database that I created with Visual studio.
I need to work on this project from another computer and copying over the project files was a breeze but I'm facing issues when it came to copying the .mdf database file.
Upon research it seems like, at least in the Microsoft SQL Server program, that I would have to "Detach" the database before copying it over to a different computer. So I am assuming I would have to do something similar with my Visual Studio Database as well.
Anyone has any inputs with regards to this?
If there is not much that I can do, I guess I could recreate all my tables and everything in Microsoft SQL Server program, so that it would be easier to move the database if needed.
I was in a similar situation such as yourself when I began developing my first core application. You have a few different options including:
Detach an already created database from the hosted SQL Server service and "re-attach" to another SQL Server service that is accessible from the desired set of hosts. You have to essentially disconnect the database from the service before you are able to transfer or migrate it since the process will have an exclusive lock on the .mdf file. https://msdn.microsoft.com/en-us/library/ms190794.aspx
Create the necessary .sql scripts to construct the database and run in the appropriate order e.g. create database, create tables, etc. to re-construct the database at the service location.The neat thing about this technique is if you have already created the database (which it sounds like you have) SQL Server allows you to generate scripts rather than having to write them yourself. https://technet.microsoft.com/en-us/library/ms178078(v=sql.105).aspx
Finally you may use a subscription based service such as SQL Server through Azure to host the service for controlled global access aka DBaaS (Database as a Service). I can't post anymore links, but look at Microsoft's Azure SQL Server hosting service if you are curious about this option.
The unfortunate part you have to decide is how much time you would like to invest in this. I began developing the application from scratch which led me to developing scripts to conjure up the database for deployment purposes. Good luck!

Entity Framework, Offline Use Strategies?

I am currently learning about EF and have come into a scenario where the data should be used in both an online and offline mode.
It is assumed that the user will have to use the system first while connected to obtain the data used offline.
I thought about serializing the queries but IQueryable/ObjectQuery are not marked as serializable.
How would I go about being able to store results from a few queries locally and then restoring them in offline mode so the use can continue to use the app? I could spend days researching this but I hope somebody can point me in the right direction.
EDIT
It is worth noting that the master SQL Server instance is a shared server that has very minimal features installed. Replication for example, is not installed.
Assuming you are using SQL Server, you vcould take a look at SQL server compact.
http://msdn.microsoft.com/en-us/data/ff687142.aspx
You could copy data from your main DB into a locally stored CE database then switch to this for offline.

C# application using SQL Server database

This might sound a silly question but I've really spend hours in trying to find a solution...
I have a local database on SQL Server and it is used with a C# application.. The only way I view the database is through SQL Server.. It is accessible and altered in my application when I am running the application on my developing PC, but when I run it on another PC the database is not found (obviously) .. How can I include the database sources when I am compiling the program, (so that it would be also found when I run it on other PCs) ?
Initially I used SQL Server CE and I achieved portability, but I had to include stored procedures, and this edition doesn't accept them :/ so I had to turn my attention to the latter type.
Sorry for my terrible English ! :(
Thanks in advance
How can I include the database sources when I am compiling the program, (so that it would be also found when I run it on other PCs) ?
You can't. SQL Server license prohibits distributing parts of it like that, plus there is no documented way to do that. If you need SQL Server on the box, it needs to come from the installer. You can create an installer for your own product, and your installer can install SQL Server if it's needed as a prerequisite.
Alternatively, you could look at other database options such as SQLite for a file-based database. You can distribute the components of SQLite.
If you need a local sql server on each pc where your application runs then you should look into the Express edition of MS SQL Server. It is the free with limitations version of MS SQL Server.

How to do a partial copy of a remote SQL Server Express database to a local SQL Server Express database?

I'm in a situation where I need to create a partial copy of a database from a remote SQL Server (2005/2008 Express Edition) and store it in a local SQL Server (2005/2008 Express Edition) database. The local copy will have the same schema definition, but contain only parts of the data from the remote database.
The local database has to be given a unique name with parameters from C# code, similar to
TodaysDate_SerialNumber_MachineNumber_DatabaseName
e.g. 20100622_1_3_DatabaseName
(Don't get caught up with the naming scheme, it hasn't been decided yet.)
I'm working in C# on .net 3.5 using VS2010.
My thoughts so far is to execute the create script for the database on the local SQL server from C# code, and then copy data from the remote database to the local database, filtered on what I actually need. The operations have to be started from C# code, but doesn't necessarily need to be all C# code. But I haven't decided yet. What do you think would be the best option for doing what I want to do?
(Btw, if I'm being unclear, just let me know and I will try to update the question with more info.)
SqlBulkCopy is probably your best bet if you're doing this entirely in code.
You'll have sql connections to each of your databases, then a select statement to run on your source database which will insert rows into a destination database/table.
The answer to this really depends on what you are most comfortable doing.
I'd probably set up a linked server on the local SQL Express and write a single SQL Script to do the whole thing. It could report progress back to the C# app using RAISERROR ... WITH NOWAIT and these messages can be processed asynchronously by setting up a SqlInfoMessageEventHandler
Maybe you can create a backup of the database, download it to the local machiene, create there a new, empty database and restore the backup to the empty database with option "override". After that you can delete those records you don't want to "copy".
You could use Microsoft SQL Management Studio to create the backup- and restore-scripts.

Web Parts & User Instance without SQL Express

I'm having hard times with the SQL Error
Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.
No matter what I do (yes, I have the SQL Express running in the Local account and deleted the SQLExpress folder on Local Profile), users under Windows 2008 always get this issue and sometimes on Vista as well (not that frequent).
I do need to find other ideas!
I was thinking move to SQLite (as there is a wonderful Provider) or even use XML file instead.
All I do is manage WebParts as panels that each user can modify their visibility and place. When done editing it saves automatically into the user instance.
I need only this functionality but I can't find a decent way to drop SQL Express 2005 User Instances ... any ideas?
Any help is highly appreciated, Thank you.
I worked for webhosting company where SQL Express was used for freehosting users. We never experienced this problem so it must be some weird combination of parameters.
I suggest to do some probing
Use Process Monitor to trace which files/directories/registry entries is SQL Express trying to access when the error occurs. Using this utility you can also locate problems with access rights.
Look into SQL Express log located in C:\Documents and Settings\user_name\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS for some additional info
Make sure you have user instances enabled running this on SQL express default instance
sp_configure 'user instances enabled', '1'
Just for sure I would try to list all user instances and eventually try to connect to one.
To list user instances connect to default one (usually named COMPUTER\SQLEXPRESS) and run:
SELECT instance_pipe_name
FROM sys.dm_os_child_instances.
Then use value in instance_pipe_name with sqlcmd utility:
C:>sqlcmd -S np:\.\pipe\69651E0A-5550-46\tsql\query.
Now you are connected to user instance. Try to execute some DML.
You probably did this but for sure have a look in Windows Event Viewer
If nothing comes up from probing you can always create database using snandard way on SQL Express default instance (connect to server, create database, setup connection string and use it instead of database.mdf in your solution).
Using "microsoft sql server compact 3.5" gives you almost any of the normal sqlexpress functionality while being easily portable without a real server to be put on.
http://www.microsoft.com/Sqlserver/2005/en/us/compact.aspx
Unless you're doing a big and complex application (lots of stored procedures, triggers and such), its a nice way to work.
Here's some other information :
http://blogs.msdn.com/stevelasker/archive/2006/11/27/sql-server-compact-edition-under-asp-net-and-iis.aspx

Categories

Resources