简体   繁体   English

通过从C#应用程序获取DBName创建数据库

[英]Create DB by taking DBName from C# application

I've a requirement to create DB by taking the DB name from the C# application. 我需要通过从C#应用程序中获取数据库名称来创建数据库。 Basically, avoiding hard-coding of DBname in the sql script. 基本上,避免在sql脚本中对DBname进行硬编码。 ex: "create Database abc". 例如:“创建数据库abc”。 In this "abc" must be passed as parameter. 在此,必须将“ abc”作为参数传递。

Once i read the DBName from the application, can anyone please suggest how to pass DBName to the script file? 从应用程序读取DBName之后,任何人都可以建议如何将DBName传递到脚本文件吗? The Script file will be read by the C# application again and executes it. 脚本文件将由C#应用程序再次读取并执行。

-Prashanth -Prashanth

You can do two things: 您可以做两件事:

  1. Use a dynamic statement, and use it to create the database: 使用动态语句,并使用它创建数据库:

     SET @sqlText = 'create database ? ...'; PREPARE stmt FROM @sqlText; EXECUTE stmt using @name; DEALLOCATE PREPARE stmt; 

    Pass in the name as parameter. 传入名称作为参数。

  2. Create the statement in .NET: 在.NET中创建语句:

     string statement = string.Format("create database {0} ...", databaseName); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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