Portability of an MDF database file across different Windows machines - c#

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.

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)

How to store SQL Server Database in my App to deploy it?

I'm very new to App deployment. This is my first one actually.
I developed an app which uses a SQL Server database to store its data. I created the SQL Server database with a script. However, neither the script nor the DB itself are part of the "package" of the app. I simply installed SQL Server Management Studio with the default settings, created a new file starting with
USE [Master]
GO
CREATE DATABASE [Database]
and filled up all the details.
The app works in my machine of course, but when I Published the app and got the .Exe, it doesn't work in any other computer, of course because it's not creating a Database to go with it, and the connection string that it uses for the queries is the one that I gave it from my SQL Server instance.
So, I want to know how to actually include the database (or a way to create it) in the "installation package", and also for the program to dynamically get the necessary connection String to operate on any computer where its installed. Right now, I AM fetching the connection string from App.Config with the ConfigurationManager, but of course, the Connection String in App.config is the one that got added when I added MY instance to the project.
Any sort of guidance would be appreciated. I searched related questions but the only ones relevant had some deprecated methods. It's still not very clear to me how it would get the connection string dynamically if its not going to be the same instance.
On a semi-related matter...I noticed that my app was being executed with just double clicking on "setup.exe". Setup seems to imply that it will only install it and add an executable...it's kind of misleading to execute the app itself from the setup, isn't there another way to change that?

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).

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

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.

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