0

I have a SQL Script File Which contains database creation, its related tables creation and other things. The database name is decided from client side. So is there any way that I can run that script file where Database name is passed from my code?

I know a way where i take some special pattern in place of database name and then in c# code I do replace it with client side added Database name. But is there any better way to do it?

5
  • I think you can read that text and execute it Commented Jun 26, 2014 at 8:50
  • @brykneval but database name will be given by user. Commented Jun 26, 2014 at 8:52
  • 1
    String.Format("Create database {0} ......", userinput); Commented Jun 26, 2014 at 8:56
  • @brykneval All script are written in a file. Commented Jun 26, 2014 at 8:58
  • 1
    you can load the text from the file, and even edit the file as Create database {0} so that it will be easy when you impport Commented Jun 26, 2014 at 9:00

1 Answer 1

1

If you are creating tables on the fly in your application, you will missed some fundamentals about database design. In a relational database, the set of tables and columns are supposed to be constant. They may change with the installation of new versions, but not during run-time.

You can check it here

create proc createdb @dbname sysname
as
declare @sql nvarchar(max)
set @sql = 'create database ' + QUOTENAME(@dbname)
exec (@sql)

Here you can pass @dbname as parameter

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.