Dhwani
Dhwani

Reputation: 7626

Run Sql Script File With Passing database name from C#

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?

Upvotes: 0

Views: 429

Answers (1)

CodingDefined
CodingDefined

Reputation: 2132

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

Upvotes: 1

Related Questions