Dump remote SQL Server database and use it locally in a file - c#

I have a website, that is based on ASP.NET and using SQL Server. It is changed every year, as it's mad entirely for a yearly event, and I want to make a website history - to make user able to open the old website in a subdomain.
My problem is that every year I have to restore the database to its original state, and I can't just create another database that would hold the old data. So what I was thinking about was dumping the entire database with its structure into a file (like .MDF) and change the web.config file, so it'd use the file instead of remote database server.
But when I use SQL Server 2012 Management Studio, I can only export a .SQL file. Is it any way to do it, or my approach is wrong?
Remarks:
Performance is not a problem, the website will be used as a showcase
I don't have admin rights on the SQL Server, just data for connections
I use Visual Studio 2013 for my (web application) project

You have several options.
use the separate databases approach in a MDF file
use separate tables in the same database eg :event_2010 ... event_2014
Don't forget to then change the domain each site responds to in IIS to a subdomain like this 2013.myawesomeevent.com and so forth for each site. And keep each installation in separate folder.

Related

What happens to database connection after software deployment (setup.exe) of C# Winforms?

I was wondering what would happen to a program's connection to the database after compiling it to setup.exe? Will the data from the server explorer be saved somewhere after producing a setup installer so it could dynamically be changed later?
I'm still a bit new in coding. Just barely started.
So I'm not really sure if my questions would make any sense to all the experts.
I hope someone could enlighten me.
Thank you.
It all depends on what form of Database and what settings you had in your project.
case 1:
you are using a server-client Database. Server-client databases are like Sql-Server, MySql, Postgresql, Oracle.....
Usually they have a host, username, password and/or authentication method.
In this case, and while you are configuring your database with in your project in visual studio, you can choose to save the connection string with in your configuration file resources or hard code it in the application.
The Data will stay on the database server.
Case 2:
You are using one of file Database solutions like Sqlite, VistaDB, Acess DB file, XML based DB files, Sql-Compact SQL-CE (obsolete and replaced with SqlExpress)...etc.
In this case, the DB file (which should be in your project file and you have chosen from visual studio the option of (copy if newer)) will be included with your project with the data in it. You have to be sure in this case the proper assemblies are present with you your application *.dll. Your installer have nothing much to do here unless the DB solution you have chosen said other wise.
For example, if you used SqlExpress your installer should be sure that it is present when your user installs your application and if not it either installs it or instruct the user (at least by providing proper links) the methods to install it.
As for the connection string it takes the same conditions of case 1.
Hence you tagged your question with #sql-server I will assume you are looking for the SqlExpress case.
If that's the case I will refer you to the couple of links which will provide you with further details:
1- How do I connect to an MDF database file?
2-How do I connect to an .mdf (Microsoft SQL Server Database File) in a simple web project?
3-C# - Connect to MDF database
4-THE ‘CONNECTION STRING’ STRING OF CONNECTION WITH DATABASE. AN EXAMPLE OF USING IN APPLICATIONS
Finally remember web search is always your friend:
5-c# sql server connection string attach database file (in google search)

Portability of an MDF database file across different Windows machines

Here's the context: I'm writing a C# application that connects to a local database file (MDF). The application's GUI enables the users to do various queries and view results in a Grid View control. The MDF file is distributed with the application and can be considered an immutable (or read-only) resource i.e. not meant to be updated or modified by the end user.
The application is currently in alpha stage and has never been deployed. The source code, along with the MDF database, is source-controlled (via Perforce), so when other developers in my team synchronize their workspace, they get a copy of the MDF database (and LDF log) along with the code. No server connection is involved, all queries are made to the local DB copy using (LocalDB)\MSSQLLocalDB and the default Windows Authentication.
Here's my problem: The MDF file cannot seem to be used anywhere but on the machine it was created on. If another developer builds my app on his computer, the connection to the local database fails with a generic SQL Server error 5171. However, the MDF file is not corrupted and is a primary DB file, so the error is elsewhere.
Here's what I tried:
Verified that SQL server version is the same on all machines, so it's not a compatibility issue
Connection string used in the application is dynamic (i.e. adapts to the location of the MDF file on the computer) and formatted as: Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\MyLocation\DatabaseFile.mdf;Integrated Security=True;Connect Timeout=30
Used same Windows login on all computers, to no avail (still gives error 5171)
Verified DB options with SQL Server Management Service: Restrict Access is set to MULTI_USER
The only workaround I found is to re-generate the whole database on the machine but that's not acceptable: can't one just connect to the MDF file one gets from Perforce and be done with it? Thanks.
Sorry to answer my own question so late after it was initially posted, but here it is: the solution is to make sure the MDF file is saved in Perforce as binary, not as text.
That solved the data corruption problem right away and the MDF database finally became portable across machines/users without any further issue.

Making database mdf work across different computers

I know this is kind of a stupid question but it gives me a lot of problems. Me and my partners in college projects have a lot of issues making the database mdf work when we send each other the visual studio projects. It gives us errors about the versions of sql server. Is the only solution to this to install same version of sql servers for the entire team or is there some other workaround?
Yes, you must be using the same version of SQL Server across all PC's.
You can never "go back" in time with a SQL Server database - once a .mdf file has been attached to a given version, it can never be attached to an older version anymore. And you cannot get around this by using the database compatibility level, either - the internal database file structures are just too different between versions, and no backwards "downgrade" path is provided.
One way to get around this would be to have a common, shared server somewhere that everyone can connect to and everyone can work with - making in unnecessary to send around "free-floating" .mdf files altogether... after all, it's SQL Server - a server-based system - not so much a file-based "database" system ....
Another way to go would be to stop sharing the binary .mdf file, but instead work with SQL scripts that you can exchange within your team, and that each team member can execute locally on their respective SQL Server instances - regardless of their local version
I suggest that instead of passing around mdf files, every person on your team creates the DB and then uses SQL scripts to create the objects. These scripts can be very easily created and exported from the SQL Server Management Studio.
This has the added bonus of being able to put the scripts under version control as part of the project.
I made such practice, with one way , by create separate pc running under windows server, only using as sql server to store all data record , then the data is called via "connectionStrings" of IP with server name of user policy, which is also secured by password and username , so this practice is used , to enabled two different programs to share that data , for example , that I made ( one web localhost app to create internal registration ),( the other web app using to access to mainpage via Login form).

How do I upgrade Umbraco CMS from using SQL Server CE to an upgraded version of SQL Server?

I would like to upgrade my Umbraco project on my localhost (and eventually my live website) from running on SQL Server CE to either SQL Server 2014 or SQL Server 2016.
The reason for the upgrade is simple: I may at some point want to manage a website that has more than 4GB of data in the database, is scalable with multiple servers, and I'd like to back things up. Otherwise I'd be lazy and leave Umbraco.sdf alone.
I have not found consistent documentation on this process anywhere. Perhaps one of you might be more experienced with SQL or Umbraco and could help out.
(Aside: For those less familiar with Umbraco, Umbraco is a Content Management System written in C# and JavaScript. There's a SQL file in here named Umbraco.sdf which contents all of the website's contents. )
I figured this out, in case anyone else gets stuck.
Here's how to do it:
Step 1: Port over your current database. With Umbraco, there's an easy way of doing this. Simply install the Export SQL Server Compact package, an addon to your Umbraco CMS. Once installed, follow the directions and generate your SQL file.
Step 2: Import the generated script to SQL Management Studio and run it in a new database. In order to do this: create a new database and give a new user permission settings to access that database (don't use your server login - you can, but it's better to create a user so you can access remotely). After that, go ahead and copy and paste your entire file (yes, that entire file) into a new query (right click the database -> run query), paste the script, and run it.
Step 3: Change the connection string. This is in your web.config. Within the XML tags of , configure something akin to this (remove the {}):
<add name="umbracoDbDSN"
connectionString="Data Source={the ip of your database};Initial Catalog={theDatabaseName};User Id={theUserId};Password={yourPassword}"
providerName="System.Data.SqlClient" />
The nice thing about this connection string is that you can edit locally, as well as on site, with the same connection string. This allows you to test code changes on your localhost, without changing the code on the website. The only thing that is linked automatically is CMS Content.
Make sure not to delete your old Umbraco connection string, in case you want to revert back to it. Simply comment that out.
Step 4: Encrypt the web.config. Clearly it's not wise to keep your database password in plaintext on your website. This MSDN on encrypting your web.config is invaluable.
There are a few things you will lose with this:
Portability of your database. Sometimes you'll want to only have a file be a database instead of a whole server. We only recommend doing this step when your site is essentially done, as configuring a SQL Server and keeping it secure is an extra challenge you shouldn't worry about.
Occasionally, images don't transport. You might have to reinput all of your images manually.
Possible security. Every time you publish your website, you may have to reencrypt the web.config manually. This can be exceedingly dangerous - so make sure your website always has the web.config encrypted, even on new publishes.
SQLCE ToolBox is the best tool for the job. Just install the extension then the steps are as follows:
In Server Explorer add the connection to your new database (Connect To Database button)
Right click on Umbraco.sdf file in in the SQLCE ToolBox and choose Migrate To SQL Server
Choose your server and export.
Modify the umbracoDbDSN connection string to point at new database.
The target database should be empty (or at least not have any conflicting table names).
It seems easiest to do it in webmatrix, have you tried that?
From: https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/53818-Convert-Umbraco-SQL-CE-database-to-SQL-Express
or: https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/49519-Changing-database-in-Umbraco-7
First you need setup a database in SQL Server. Once that is done, then open the Webmatrix, and in the lower left corner in the webmatrix UI you can select the databas. Then browse to the SQL Server CE database, it's located in the \App_Data folder after that a "migrate button" will appear in the upper menu options. By press that button a migrate dialogue appears asking for connection details to the SQL Server database that you have created.

changing the location of C# code files along with db

I have an application written in c# [WPF] which involves lot of communication with several databases, hence i used SQL server 2008. Now i need to move the code files along with the database files to another system(it doesn't have sql server installed). Once i run the applications exe , it gets struck as it is not able to retrieve data from .mdf file.
Is it possible to access sql server 2008 installed in my system through a network ?
It is pretty simple . I think you are using .mdf files as your database. Instead of it use .sdf as the database format which is mostly used in mobile development. There are lots of .mdf to .sdf converter available in the net.
You can have the second system access the SQL Server of the first system.
You need to modify the connection string so that it accesses a server on a remote machine, and make sure the proper authentication mechanism is used. If both machines are on the same Winodws domain, it's going to be fairly simple.

Categories