Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I am trying to create a database programmatically with the following code:
C#
@Select = @Select + sql;
String str;
SqlConnection myConn = new SqlConnection(<a valid="" connection="" string="">);

str = "CREATE DATABASE " + sql + " ON PRIMARY " +
      "(NAME = " + sql + ", " +
      "FILENAME = '" + @Select + ".mdf', " +
      "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
      "LOG ON (NAME = " + sql + "_Log, " +
      "FILENAME = '" + @Select + ".ldf', " +
      "SIZE = 1MB, " +
      "MAXSIZE = 5MB, " +
      "FILEGROWTH = 10%)";

where the variable sql is a valid database name ("ChaosSpaceMarines").
@Select is a valid path ("E:\\databases\\")

This code works fine once. But if I delete the database ChaosSpaceMarines (and all associated files) I get the SQL error "Database ChaosSpaceMarines" already exists. Even if I create the new database on a different drive. (Also tried a different directory).

Can anybody tell me why I can only create the named database only once? The database contains 22 tables and currently I am trying to debug the SQL statements that create the tables. It is a pain to have to use a different table name each time I restart the program.
Posted
Updated 26-May-14 6:32am
v2
Comments
[no name] 26-May-14 12:11pm    
Are you deleting the database correctly or are you just deleting the files? SQL Server would still "know" about the database if you just delete the files.
Member 10599190 30-May-14 12:40pm    
Excellent point. That must have been the problem.
[no name] 26-May-14 12:11pm    
It seems you do not use DROP DATABASE to delete the db
Member 10599190 30-May-14 12:44pm    
Indeed not. And as I just discovered, that was the problem. I just deleted the files on the drive without dropping the database from the server as somebody already pointed out.
Show how you exactly delete the Database.

You can't add another database with same name even if its path is differ.
Have a look here: CREATE DATABASE[^]

More details: Delete a Database[^]

MSDN wrote:
Follow Up: After deleting a database

Back up the master database. If master must be restored, any database that has been deleted since the last backup of master will still have references in the system catalog views and may cause error messages to be raised.
 
Share this answer
 
v2
try
{
@Select = @Select + txtDatabase.Text;
str = "USE MASTER";
myCommand = new SqlCommand(str, myConn);
myConn.Open();
myCommand.ExecuteNonQuery();
str = "DROP DATABASE " + sql;
myCommand = new SqlCommand(str, myConn);
myCommand.ExecuteNonQuery();
myConn.Close();
}
catch (Exception) { myConn.Close(); }

With this try block (and subsequent catch, of course) I am able to "drop" the named database from the Master table and recreate the database. This is not a very elegant solution, but hey, it works.

Thank you one and all for your excellent suggestions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900