开发者

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

开发者 https://www.devze.com 2023-03-27 06:49 出处:网络
Here is my code: String str; SqlConnection myConn = new SqlConnection("Server=localhost;Integratedsecurity=SSPI;database=master");

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(), "M开发者_如何学运维yProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
   if (myConn.State == ConnectionState.Open)
   {
      myConn.Close();
   }
}

Here is the error:

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

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


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


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.


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


Try providing the Username and password in the connection string

0

精彩评论

暂无评论...
验证码 换一张
取 消