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").

5
  • 1
    What kind of database? Commented Jan 27, 2016 at 12:02
  • Microsoft SQL Server database, but it would be even better to let the connection string decide. Commented Jan 27, 2016 at 12:17
  • msdn.microsoft.com/en-us/library/bb748675(v=pandp.31).aspx Commented Jan 27, 2016 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" ;-) Commented Jan 27, 2016 at 12:20
  • Just an idea, how about some powershell? It would be simple to parse the con string, then use something like stackoverflow.com/questions/8048822/… or sqlblog.com/blogs/allen_white/archive/2008/04/28/… Commented Jan 27, 2016 at 13:09

3 Answers 3

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;
        }

    }
Sign up to request clarification or add additional context in comments.

1 Comment

You should dispose of the connection and the command object when you're done with them
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();
    }
}

Comments

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.