Managing multiple customer databases in ASP.NET MVC application - c#

I am building an application that requires separate SQL Server databases for each customer.
To achieve this, I need to be able to create a new customer folder, put a copy of a prototype database in the folder, change the name of the database, and attach it as a new "database instance" to SQL Server. The prototype database contains all of the required table, field and index definitions, but no data records. I will be using SMO to manage attaching, detaching and renaming the databases.
In the process of creating the prototype database, I tried attaching a copy of the database (companion .MDF, .LDF pair) to SQL Server, using Sql Server Management Studio, and discovered that SSMS expects the database to reside in
c:\program files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\MyDatabaseName.MDF
Is this a "feature" of SQL Server? Is there a way to manage individual databases in separate directories? Or am I going to have to put all of the customer databases in the same directory? (I was hoping for a little better control than this).
NOTE: I am currently using SQL Server Express, but for testing purposes only. The production database will be SQL Server 2008, Enterprise version. So "User Instances" are not an option.

The MDF has in it the table containing the physical path of all the database files, as they were on the instance it was detached from. You can overwrite the location(s) during the attach operation:
CREATE DATABASE <dbname>
ON (name=dbfilelogicalname, filename='c:\myNewPath\dbfilename.mdf'),
(name=dbfile2logicalname, filename='c:\myNewPath\dbfilename2.ndf'),
(name=dbloglogicalname, filename='c:\myNewPath\dblogfilename.ldf')
FOR ATTACH;

Related

Locate database

I have used SQL Server Management Studio to convert an Access file to SQL Server. I want to add this database to a project using C# (it appears in Server Explorer - Data Connections).
When I use C#, Solution Explorer - Properties - AddResource - Add Existing file I am required to navigate to the database file, but I cannot locate it.
Database is RWJ-PC.PaulsNewDatabase.dbo. Please advise if possible.
Somewhere on your hard disk (or in a network folder)is a file with an extension of mdf. Attach the file to your SQL Server (using SQL Server Management Studio) instance and you will have a a database. SQL Server databases live on the file system as a mdf and (possible one or more ndf) file(s). It lives in memory and can be used when attached to a SQL Server instance.
C:\ProgramFiles\MicrosoftSQLServer\MSSQL12.MSSQLServer\MSSQL\Data\Filename
Found eventually by trolling the internet
If you have imported the data into an instance of SQL server you can connect to the instance of SQL or to the mdf. Look here --
How do I connect to an MDF database file?
Search for connecting to SQL Server with C# (VB.Net). You can use Entity Frame work or connect directly.

Easy way to keep two SQL Servers synced if you have read only access to publisher (source) of data

I am making C# app that rely on data from one old SQL Server 2005 machine.
Since I have only acces to read only data from that server, I need to build up some kind of handmade replication.
My app is going to use SQL Server 2012 and I am planing to read data from old SQL server in nightly tasks.
Before I start reinventing the well for sync data between two SQL Servers, I'll love to try to find some kind of library or system which can do the JOB.
Unfortunately I can't just setup replication between two SQL Servers because source of data is at SQL Server 2005 version and I do not have admin rights on that server.
I just need few tables to keep sync (updated) at my new SQL Server.
Is there some kind of embedded replication which can be called from code, and which have no needs for writing and admin access to publisher database?

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.

Visual Studio: Sql Server in-project DB

I would like to create a new VisualStudio project with a simple SQL server db, but i want it to be portable, because several people will need to have access to the DB.
So it should be also accessible from a user with password.
The point is that when i create a Local SQL server DB i can't create a new user because it sais You can only create a user with password in a contained DB.
Is there a way to create a Local DB with password or a way to let several user access to the same DB?
Thx
Is there a way to create a Local DB with password
Yes. I'd recommend using Sql Server CE or SqlLite for a database like this. If you're only using Sql Server as a data store on the local machine, Sql Server, even Express Edition, which is still the full Sql Server engine, is usually way overkill for this. The full Sql Server engine really only makes sense when the database engine is the sole, exclusive reason that the machine where it runs exists.
Is there... a way to let several user access to the same DB?
Yes. Install Sql Server on a dedicated database machine, instead of using a Local DB, and set the appropriate connection string and permissions.
Here's the trick: you can't do both, at least not without going through all the same trouble you'd go through setting up a real Sql Server to run on the machine where this will be installed.

How to use backup .sql file to add databse to sql server using C# console application?

I was working on a project where i made a database on sql server 2008 using visual studio 2010 and i took backup of that databse by right clicking on it in sever explorer and selecting "Publish to Provider" that creates a .sql file in my computer that i taken. Previously i formatted my pc and sql server databases are lost, so now i want to use that .sql file to recreate the database with all data, but i don't know how it can be possible.
Something i tried but it created all tables in master database but i want the database of the name i.e. stored in .sql file or specified by me.
Please help.
First Create a Database using this,
Create Database YOURDBNAME
Here, YOURDBNAME = DataBase Name
Then, Open your .sql file write this in First line of the .sql page,
Use YOURDBNAME
Now press F5, all the tables and data will be created and inserted.
You can use the SQL Server Management Objects libraries to manage all aspects of SQL Server.
SQL Server Management Objects (SMO) is a collection of objects that are designed for programming all aspects of managing Microsoft SQL Server. SQL Server Replication Management Objects (RMO) is a collection of objects that encapsulates SQL Server replication management.
From your description, however, this can be overkill. You get simply use SQL Server Management Studio (link goes to the free Express edition download), connect to the server, open a new Query Window with your SQL file and run it (F5).

Categories