Access SQL Server database without SQL Server service - c#

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.

Related

Database backup of azure database deployed in local sql server management studio

Can anyone help me in doing this task? I am using my sql azure database in my local machine's sql server management studio 2008 r2. What my issue is, I am trying to take backup of a database from my c# console application using the following methods:
using smo: showing error at "sqlBackup(server)" method.
The error details like -
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Microsoft.SqlServer.Management.Smo.SqlPropertyMetadataProvider.PropertyNam
eToIDLookupWithException(String propertyName, PropertyAccessPurpose pap)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetDbComparer(Boolean inSe
rver)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitializeStringComparer()
at Microsoft.SqlServer.Management.Smo.AbstractCollectionBase.get_StringCompar
er()
at Microsoft.SqlServer.Management.Smo.SimpleObjectCollectionBase.InitInnerCol
lection()
at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.InitializeChildCollec
tion(Boolean refresh)
at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.GetEnumerator()
using "backup database" command, this showing command not supported in this version of sql server. After searching in internet, i found that this command wont support for azure databases in ssms.
Can anyone please provide me solution to solve this.
You can not backup the Database in Azure Sql Service. When try to Backup Shows this Error
Statement 'BACKUP DATABASE' is not supported in this version of SQL Server.
So Backup Azure SQL Service by
Right Click on Db --> Tasks --> Export data Tier Application --> Set Location
Creates a bacpac file after export, then it can be import via this
Note: Be aware there are export and extract. The extract option only copies the schema, so if you need the data as wel make sure to use the export variant.
For security reasons, SQL server instance doesn't allow database migrations between two different target servers such as local and azure or vice versa.
The below tool can help run the scripts on one server to another. Keep in mind that when you run them it restores the schema and the data fully. You can alter the settings in Advanced option.
https://sqlazuremw.codeplex.com/

How can I deploy a Sql Server database as part of an application's setup?

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.

How to maintain database in a separate (portable) SQL file?

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

Backup and restore MS SQL database

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

Is there a performance hit for using MDF SQL Server files instead of "database"?

Currently my website is written in ASP.NET Webforms using a SQL Server database. I am planning to build a ASP.NET MVC application not because it's better but because I want to learn the technology. My question is more specific to the database. I can create the database and import my SQL table via the import feature of the web interface to the SQL database or I can use a "local" database MDF file. I like the idea of using a MDF file because it will be easier to backup and deal with. My website doesn't get a lot of traffic... matter of fact I could be the only user. :) Now here is the question.
How much of a performance hit will I get by using a local SQL Server MDF file instead for my database?
Effectively none. Or really none more than having the DB run on the same box as the web site. It is still a Sql Server Express database all the same. Production-wise, you probably want the DB running on a separate box dedicated to serving databases. But code-wise, the only difference will be your connection string.
SQL Server uses a MDF file for each database on its server. There is no difference between a MDF file and a 'database' because the 'database' gets stored in a MDF file anyway.
Performance wise you should not see a difference.
The biggest issue is with production deployment and management. It is MUCH easier to manage a standard database, than a dynamically attached .mdf.
Also, don't forget that your web host has to support this as well. And since SQL Server Express is the only SKU that supports "user instance" databases, the host will have to have Express installed for you to use it as-is. OTOH, you can develop with it this way and then just deploy your database and change the connection string when you deploy to the web host.
The only difference beyond the normal resource limits of the Express version of SQL Server is a negligable startup cost while the SQL Express engine connects to the MDF file, does its routine checks for file integrity and transaction log stuff.
This should only happen on application start up, not for every request.

Categories

Resources