up vote 1 down vote favorite
share [g+] share [fb]

Here is my code:

String str;
SqlConnection myConn = new SqlConnection("Server=localhost;Integratedsecurity=SSPI;database=master");             

str = "CREATE DATABASE JoesData ON PRIMARY " +
      "(NAME = JoesData, " +
      "FILENAME = 'C:\\JoesData.mdf', " +
      "SIZE = 3MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
      "LOG ON (NAME = MyDatabase_Log, " +
      "FILENAME = 'C:\\MyJLog.ldf', " +
      "SIZE = 1MB, " +
      "MAXSIZE = 5MB, " +
      "FILEGROWTH = 10%)";

SqlCommand myCommand = new SqlCommand(str, myConn);
try
{
   myConn.Open();
   myCommand.ExecuteNonQuery();
   MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.Exception ex)
{
   MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
   if (myConn.State == ConnectionState.Open)
   {
      myConn.Close();
   }
}

Here is the error: enter image description here

Any idea what might be causing this? Thanks in advance for any help.

link|improve this question
1  
Please paste the text of the error into your question. – SLaks Aug 10 '11 at 15:26
2  
You can write error code here instead of flickr photo :-) – Soner Gönül Aug 10 '11 at 15:26
4  
The error says that the SQL Server instance was not found or was not accessible. That should be pretty self explanatory for the origin of the problem => your SQL Server instance was not found or was not accessible. – Darin Dimitrov Aug 10 '11 at 15:26
Does the script work if you run it in MS SSMS? Do you have a named instance? .\SQLExpress for example. – Jason James Aug 10 '11 at 15:27
Also, please update your title to be more specific about the error you are getting. – Shane Wealti Aug 10 '11 at 15:29
show 1 more comment
feedback

4 Answers

Your database connection is failing. Check your installation and your connection string.

link|improve this answer
feedback

I'd say you can't connect to the default SQL Server instance running on the host machine.

This can be for a variety of reasons.

Its not installed and started, maybe you don't have the right priviliges? Is there a problem with the Integrated Security, are you in the right server role?

It could even be more obscure, say you have localhost mapped to a remote IP address in you hosts file but, that is less likely.

link|improve this answer
feedback

Could it be as silly as this typo in your connection string??

Instead of:

Server=localhost;Integratedsecurity=SSPI;database=master

try:

Server=localhost;Integrated Security=SSPI;database=master

note the SPACE between the Integrated and Security

link|improve this answer
feedback

Try providing the Username and password in the connection string

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.