Link to home
Start Free TrialLog in
Avatar of Yampolka
Yampolka

asked on

Create Databse dynamically with name which send as a paramert

I  need to created Database  dynamically with the name which send me as a parameter
so it's should be dynamic statement
then I should attach mdf , ldf files  which contain database structure and sp proc
I should run this from .net app
What is the best way to do it?
Avatar of Mark Wills
Mark Wills
Flag of Australia image

Have a read of the link below. It has a few references to other languages as well...

https://support.microsoft.com/en-us/help/305079/how-to-create-a-sql-server-database-programmatically-by-using-ado-net
The best approach probably is to write appropriate methods in a class and a single method which would do the job, to which you will pass the parameter which is a database name.

I don't see anything which is hugely complicated here. Except probably a part in which you are saying that you will attach mdb + ldf which would have structure. This is all fine, but you will need to keep on renaming the template mdf + ldf files so as to have a unique name each time.

If it is possible, it would be nicer to place all of this in a model database, which is the one get copied over while creating the new database. So second part is not required at all.
The native concept SQL Server provides for that is dacpac/bacpac. (Extract/Export Data-tier application). You should make use of that instead of mdf/ldf attaching.

You can maintain and version these pacs in VS and have, compute diff scripts of two dacpacs etc. Very nice concept to maintain different db versions and updates and also good for the initialization.

Just notice in advance, before stumbling uppon it, the DAC Deploy wizard of SSMS has it's limitations, so use sqlcmd.exe andsqlpackage.exe for deployment.

I won't go into detail, just google dacpac and you'll get staarting points for what'S necessary to know.

Bye, Olaf.
If you use the restore database command with the wtih move clause, baesd on a full backup of the original database, you can do the creation, renaming and attaching in one go. The above stated is correct - you need to make sure you either use different folders for each DB, or rename the files to unique names (usually the db name), to avoid conflicts.
A copy of the model database is used to create new databases. If you create your future databases structure in the model database then you'll only need the new database name to be sent as parameter and all that you'll need to do is to run the CREATE DATABASE command:
CREATE DATABASE newDBname

Open in new window

Guys, modifying the model DB is a quite uncommon and useless feature. Most of the time you do not want to have only copies of the same database structure, but different databases, in your instance.
@Qlemo, that really depends on how your organization works.
I've been working with SQL Server from long and mostly in finance institutions where you have hundreds to thousands of SQL Server instances. Organizing those SQL Server instances logically so you can distribute your databases through those SQL Server instances, is the first step of a big strategy. Inside the logic that you use (application, customer department, business area, etc...), you can easily have a specific model database for each SQL Server instance or group of SQL Server instance.
So, what's uncommon and useless to you, doesn't mean that it is for others.
Avatar of Yampolka
Yampolka

ASKER

I need to create database with special rights  and security requirements.
I  have a text file with    database script. I can modify here database name, rights etc.
Is it possible  to run this script from ASP.NET application?
All you need to do is construct and run a T-SQL command, so yes, ASP.Net works.
ASKER CERTIFIED SOLUTION
Avatar of Mark Wills
Mark Wills
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you , Mark
It was extremely  helpful , you provide detailed instructions..