c# - Simplest way to create database from connection string? -
we're looking hands-on way create database given connection string minimum code possible (e.g., include in linqpad scripts or c# scriptlets).
is there one-liner or similar create database connection string? preferably "static.createdatabase("connection string")
.
i have created static class scripts contains static method createdatabase
// use class scripts.createdatabase("connectionstring") //class static class scripts { static bool createdatabase(string connectionstr) { bool result =false; sqlconnection conn = new sqlconnection(connectionstr); // pass connection string , user must have permission create database, string query = "create database exampledatabase "; sqlcommand command = new sqlcommand(query, conn); try { conn .open(); command.executenonquery(); result =true; } catch (system.exception ex) { result =false; } { if (conn.state == connectionstate.open) { conn.close(); } } return result; } }
Comments
Post a Comment