Create DB by taking DBName from C# application

140 views Asked by At

I've a requirement to create DB by taking the DB name from the C# application. Basically, avoiding hard-coding of DBname in the sql script. ex: "create Database abc". In this "abc" must be passed as parameter.

Once i read the DBName from the application, can anyone please suggest how to pass DBName to the script file? The Script file will be read by the C# application again and executes it.

-Prashanth

1

There are 1 answers

1
Patrick Hofman On

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:

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