Joe
Joe

Reputation: 27

C# and SQL Server error with "CREATE DATABASE" statement

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.

Upvotes: 1

Views: 865

Answers (4)

Tanmay
Tanmay

Reputation: 351

Try providing the Username and password in the connection string

Upvotes: 0

marc_s
marc_s

Reputation: 755207

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

Upvotes: 0

Jodrell
Jodrell

Reputation: 35726

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.

Upvotes: 0

Blazes
Blazes

Reputation: 4779

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

Upvotes: 5

Related Questions