2

We're looking for a hands-on way to create a database from a given connection string with minimum code possible (e.g., to include in LINQPad scripts or C# scriptlets).

Is there a one-liner or something similar to create a database from a connection string? Most preferably something like "Static.CreateDatabase("connection string").

D.R.
  • 20,268
  • 21
  • 102
  • 205
  • 1
    What kind of database? – stuartd Jan 27 '16 at 12:02
  • Microsoft SQL Server database, but it would be even better to let the connection string decide. – D.R. Jan 27 '16 at 12:17
  • https://msdn.microsoft.com/en-us/library/bb748675(v=pandp.31).aspx – Amit Kumar Ghosh Jan 27 '16 at 12:18
  • @AmitKumarGhosh: This looks simple, however, linking the P&P Enterprise Library is not what we think of as "lightweight to use in scripts" ;-) – D.R. Jan 27 '16 at 12:20
  • Just an idea, how about some powershell? It would be simple to parse the con string, then use something like http://stackoverflow.com/questions/8048822/is-it-possible-to-create-a-database-in-sql-server-with-powershell or http://sqlblog.com/blogs/allen_white/archive/2008/04/28/create-database-from-powershell.aspx – Liesel Jan 27 '16 at 13:09

3 Answers3

3

I have created a static class Scripts which contains a static method CreateDatabase

// USE Class Like this
Scripts.CreateDatabase("Connectionstring")

//Class
static class Scripts
    {

        static bool CreateDatabase(string Connectionstr)
        {
            bool result =false;

              SqlConnection Conn = new SqlConnection(Connectionstr); // pass connection string and user must have the permission to create a 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;
                }
                finally
                {
                    if (Conn.State == ConnectionState.Open)
                    {
                        Conn.Close();
                    }
                }
            return result;
        }

    }
0
SqlConnection myConn = new SqlConnection ("Server=localhost;Integrated security=SSPI;database=master");
String sql = "CREATE DATABASE MyDatabase";
SqlCommand myCommand = new SqlCommand(sql, myConn);
try {
    myConn.Open();
    myCommand.ExecuteNonQuery();
    // OK
} catch (System.Exception ex) {
    // failed
} finally {
    if (myConn.State == ConnectionState.Open) {
        myConn.Close();
    }
}
Igor Gorjanc
  • 535
  • 4
  • 17
0

Download nuget add as a reference to LINQPad.

Then use comand:

void Main()
{
    var database = new ProductivityTools.CreateSQLServerDatabase.Database("XXX", "Server=.\\SQL2019;Trusted_Connection=True;"); 
    database.Create();
}
Pawel Wujczyk
  • 422
  • 3
  • 12