简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM