Recently I asked in the Forums for help in creating a new database on a SQL Server from within my application. I thought this would be helpful when I installed a custom app at work, where I don't have access to the server, but have permissions to administer the SQL Server 2008 service.
I received a number of helpful suggestions, but they all boiled down to "you can't do it." I hate being told "No" so I searched some more. MSDN was useless, and Google not much better, but I finally found an answer in the Microsoft Technical Support Knowledge Base.
Just in case someone else is looking for a way to do this simple task, I offer the following simple code:
public partial class Form1 : Form
private void btnCreateDatabase_Click(object sender, EventArgs e)
SqlConnection myConn = new SqlConnection
("Server=BAAL\\SQLEXPRESS2;Integrated security=SSPI ;database=master");
str = "CREATE DATABASE MyDatabase";
SqlCommand myCommand = new SqlCommand(str, myConn);
MessageBox.Show("Database Created Successfully", "MyApp",
catch (System.Exception ex)
MessageBox.Show(ex.ToString(), "MyApp", MessageBoxButtons.OK,
if (myConn.State == ConnectionState.Open)
I apologize for the formatting - Opera is a bit user-hostile in that respect.
Your app must be running under an account that has full privileges on the server for this to work, and you'll need to modify the connection string to match your installation, of course. But this worked perfectly on the first attempt, and I thought it worth sharing.
BS Electrical & Electronics Engineering
Thirteen years experience designing & programming measurement & control instrumentation.
Seven years working as Systems Manager for a local Ace Hardware chain.
Currently employed by the local Indian tribe at their electric utility, building substations, automating meter reading, mapping the system and inventory with GPS, and now that we've absorbed the water and sewer system, designing sewer and water lines, sewer lift stations, and developing wells.
Programming experience using assembly, FORTRAN, BASIC, HP JCL, Prolog, Ada, Pascal, hpl, ; currently learning VC# and having "fun" with ASP.Net.
Avid darts player, though not a good one... Fair to excellent 12-string guitar player, depending on who you ask. Spare time spent fishing, reading, and playing with the neighbor's cat (see picture). Second degree black belts in Hapkido and Taekwondo.
Past President of the local Kiwanis Club, past Director of the United Way, and no longer a member of either.
Have held licenses as a Real Estate broker and Securities dealer - once ordained as a minister on a whim. My latest certificate was earned without any hands on experience at all - Yamaha Certified 5-Star Technician for motorcycles, ATVs, and watercraft. What next?
Raised in sunny California, before it became the Peoples' Republic of California. Relocated to Arizona to escape being homeless after peace broke out.
Objective: To learn all there is to know. I know I'll be disappointed one day, but I've made a good start, and the eventual disappointment is likely to be brief.