Situation is simple.
I have application in C# that use MS SQL Database.
For some reason i need to add some backup/restore function.
I don't have directly access to MS SQL instance.
Is there any other simply way to backup database from C#?
Maybe some script can create tables, structures and data, that can be use on other machine?
I need to do this only via C# (standard .NET references). No third party applications.
No, there isn't.
If you don't have permissions or even a login to the SQL Server instance, you will not be able to run any sort of BACKUP commands against the database.
Backing up the file system (the mdf, ldf, and ndf's) isn't a sufficient backup strategory for SQL Server.
You can use SMO for this (provided you have the appropriate access permissions):
To run the SqlBackup method, users must have BACKUP DATABASE or BACKUP
LOG permissions on the database, or be a member of the db_owner and
db_backupoperator fixed database role and the sysadmin fixed server
role.
Also:
C# SMO backup of remote database to local machine.
Getting Started with SMO in SQL 2005 - Backups
Related
My C# .NET (4.5.2) application accesses a local SQL Server (13.0.1601) database.
For data archival purposes my client wants to pack a snapshot of the program and the database in a folder to run the old state when needed.
How can this be achieved with no running SQL Server service but only the folder contents and an installed .NET runtime?
Options considered so far are exporting the database to .bak or .csv and putting them in the folder, but those seem to involve a lot of manual labor.
As mentioned in the comments by marc, you need to have a SQL instance running to use a backup file.
Exporting it to a .bak file is not a "lot of work" - it just involves logging in to the SQL Server (from the application) and issuing the right SQL commands.
But in order to view this again you will have to re-import it back into a SQL instance - this is the reverse of the step above (although obviously you will have to rename it because your original database will still be resident in the SQL instance).
If you are looking to avoid having a SQL instance (service) installed then maybe SQL Server Express LocalDB is an option for you - it runs totally in memory.
You could create a script to export the tables as CSV and modify the program to use q to run its sql queries directly to those CSV files.
There is no embedded version of SQL Server. The closest thing is the LocalDB feature available since the 2012 version - it does require installation of some SQL Server components but does not require running a service. With it installed, you'd attach the MDF and LDF files (read-only if necessary) of the snapshot.
I want to restore a (not yet existing) database from a .bak file into a remote database server via a C# (.Net 4.0) program.
I know it is not possible via an SQL script to do so, because the .bak file needs to be located on the SQL Server machine. Is it possible via C# though?
Basically i want this:
public bool RestoreDatabase(FileInfo backupFile, string connectionString, string dbName)
{
// Magically Restore Database
// Throw Exception on error (Db already exists, file not found, etc)
}
Before i invest hours of programming and investigation, i want to know if it is technically possible, or if i have to transfer the .bak file to the SQL server.
EDIT:
The context is, that i want to minimize "manual" work that needs to be done in my current project. Currently, when installing the software in a new environment, someone has to manually install the databases by copying the .bak file to the SQL Server, opening SQL Server Manager and restoring the database there. 2 databases are needed, and those MIGHT be placed on 2 different SQL Servers.
My idea was to run 1 programm on ANY system in the same network as the SQL Servers, entering SQL Login credentials, and restoring the databases from the one system, without any network drive. Since this would again mean manual configuration (copy .bak file to network drive, enable SQL server to access network drive [actually then you can just copy the file directly to the SQL server]), i want to avoid it.
EDIT2:
Due to different context-related issues, i cannot export the database as SQL/DACPAC/Snapshot. It has to be .bak sadly.
You asked "Is it possible via C# though?".
The answer is no, it isn't possible via C#.
As #Mithrandir says in the comments, the computer running SQL Server must be able to access the physical backup file somehow. That means the file either needs to be located on that computer somewhere, or it must reside on a file share to which the computer has access.
Another option is to generate sql scripts that create the whole database even with initial data as INSERT statements. You do not need to transfer any BAK file then.
I am creating an application for some user to maintain records in database. For this, I'll have to write SQL query (C#) and create the database, if does not exist, when user starts/installs the application. To make the creation and backup procedure easier, I want to create a separate file for SQL Server that will be used to store data. This file will be included in installation pack and copied to the destination folder to be used by SQL Server.
I've seen that we can create such file but never used like this.
Is it possible to accomplish the job I am trying to do?
i think that if each application have its own db you should use or SqlCe or SqlLite
They are a self-contained, serverless, zero-configuration, transactional SQL database engine.
So you don't have to install sql express on every pc.
They use a subset of tsql and you can do almost the same thing as sql server
you could embed a file in the application with all the sql command to create the db and then execute it the first time the application start.
Anyway you can distribute your app with the db already created and ready to use: it is just a .sdf file
If you think to distribute your app with clickonce than Sqlce is better becouse clickonce recognize its file format and handle it during the application updates
If you want to use SQL Server, you can use the Compact Edition: http://msdn.microsoft.com/en-us/data/ff687142
SQL Server CE databases are stored in .sdf files (up to 4GB) that can be shipped with your application. That way, if you want to connect to a full SQL Server database later, you could just change the connection strings in your application config.
You could make a backup and then restore it http://www.dotnetspider.com/forum/162986-database-backup-restore-through-C.aspx
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.
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.