detach an SQL server from visual studio - c#

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!

Related

What is the most efficient way to deploy a WPF application together with a SQL Server database?

I am building a WPF application that connects to a SQL Server database. I am wondering how I should go about, deploying the application on the client's computers together with the local database.
My initial thoughts are (assuming that the client computers have SQL Server installed):
Deliver the .exe file of the application
Provide a script file that runs my stored procedures for creating the database and tables
Is there a better way of doing this? I could create a service inside the application that sets some sort of boolean value in the registry of the computer and runs the "init-database-scripts" only the first time but it feels wrong.
Any other suggestions?
Thank you!
First off, I would strongly consider making an actual installer using a tool like WiX. It can run the script for you at install time so you don't have to deal with the registry and such.
There is another option assuming you are using Entity Framework - Code First; you can use a database initializer to create your database. If you never think your model will change DropCreateDatabaseIfModelChanges will work, otherwise enable migrations and use a migration initializer.
To use the initializer you picked, make sure to call Database.SetInitializer in your applications startup routine.
Add a SQL Server database project to your Visual Studio solution. When you build the project, a Dacpac file is created which can be installed on a client machine using SQLPackage.exe. Basically, your app installation script/software calls SQLPackage along with your Dacpac file to install/update the SQL database. Here is a link to a tutorial on setting up a SQL database project: Tutorial

WPF C# MS SQL - DesktopVersion and Save/Load data to file

I have an application that currently runs in MS SQL Server Standard in multiple clients/local server. The program was builded using WPF + StoredProcedures/functions/triggers, and there aren't MS Jobs.
I want to create an desktop version of my application, where the user can save all the data to a file and load it in another desktop with the program running.
I never done this type of application before, and I have a few doubts about how I should do this, I don't know if what I'm planning is going to work.
Basically, I was thinking in install my application with SQL Server Express and to save/restore it to files, I thought about calling backup/restore database through storedprocedures.
Am I thinking right? Is there a better way to do this?
I'm not sure I understand why you would need to export the entire contents of the database to a file. The central database should be accessible by all of the client applications, avoiding the need for the client application to have its own dedicated copy of the database. But ... if this is really what you want, then I think your two options are:
1.) Use Backup+Restore.
2.) Use LocalDB with the AttachDbFileName property, which is designed to keep your database in a single self-contained local file that you can read/write without having to connect to an actual database server. Some starting info here.

Windows Form App - Which database type should I use?

I'm hoping someone can help me. I recently started the development of a windows form application connecting to a remote sql server database. I was happy enough developing it until a potential client queried if they would be able to buy the whole application but they do not want the application connecting to the db via the internet.
I predominantly develop websites using php/mysql but migrated to c# for this particular project. I'm familiar with sql but not sure what database I should be using if the client wishes to have the whole application on their own computer. I've considered providing the database install as a pre-requisite when publishing the app (although I'm currently not sure how to do that) but I'm having reservations whether that is suitable or could lead to more problems. I want to create an application that can install to a single computer and has little to no need for administration. Could someone advise the best way to approach the data storage in this instance.
Because you have already a SqlServer database operating on your remote site, the best path should be to use the LocalDB version of SqlServer Express 2012. See this link about deployment.
If you don't use stored procedures, views and triggers then also the SQL CE could be an option, but you will not have file binary compatibility and you should work on importing your schema and data.
SQL CE is a compact light weight way of going..
http://blogs.msdn.com/b/sqlservercompact/archive/2011/01/12/microsoft-sql-server-compact-4-0-is-available-for-download.aspx

Entity Framework: existing database -> data model -> new database

I have created a new MVC3 project and used the ADO.NET Entity Data Model to map out my existing database. It worked great.
Now, that is a database I don't want to affect when doing development of my web app, so I was looking to now attach that data model to a local database (haven't selected one yet, suggestions welcome. I was thinking SQL Server CE or MySQL).
Does anyone have any ideas on how to do this? I'd be nice to keep the existing connection string to my live database as well as this development database environment.
You can generate another matching DB using whatever technology you want (though it would be good to keep them similar to your production DB) and then have a Web.Debug and a Web.Release setup.
If you look at swapping out the configuration, connection string portion of your web.config file you can easily swap from development to production.
http://msdn.microsoft.com/en-us/library/dd465326.aspx
This provides the relevant information for the transform statements in your Web.config file, but from the sounds of it you can simply swap the connection portion of your web.config and get what you want.
Is the existing database on your machine, or on a server somewhere?
If it's on your machine, just back it up and restore it to a different database; i.e. if your database is called XYZ, back it up and restore it to a database called XYZ-Testing. You can then connect to and use XYZ-testing without affecting XYZ.
If the existing database is on a server somewhere, the easiest solution is to install SQL Express on your machine, and then backup the database on the server and restore it on your machine.
Doing this will make your life much easier than trying to use a different database type (SQL CE or MySQL).
Another option would be to use SQL Server Data Tools (SSDT) free download, add-in to VS or stand-alone if no VS, and then to create a local database project for development. I wrote a blog post about using SSDT as well.

C# SQL Server Express backup and restore

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.

Categories

Resources