How i can delete database from local server without database files

328 views Asked by At

If database files .mdf and .log do not exist in folder with application then i need to create new database, but if i do like this

CREATE DATABASE Mydvds.mdf

And i have exception that databese Mydvds.mdf is alredy exist. If i try to drop database like this

DROP DATABASE Mydvds.mdf

I have exception that base is not exist or i don't have premmisions. I was checking database list by way of

SELECT * FROM sysdatabases

And C:\Mydvds.mdf in this list. How i can remove this database from server? Or can you tell me about another way do this?

        public void create_db()
    {
        string base_path = Environment.CurrentDirectory + @"..\..\..\My_cool_db.mdf";
        string log_path = Environment.CurrentDirectory + @"..\..\..\My_cool_db_log.ldf";

        string ConnectionString = @"Data Source=(LocalDB)\v11.0";
        string cmnd_create_db;
             connection = new SqlConnection(ConnectionString);

             cmnd_create_db = "CREATE DATABASE My_cool_db ON PRIMARY " +
             "(NAME = My_cool_db, " +
             "FILENAME = '" + @base_path + "', " +
             "SIZE = 4MB, MAXSIZE = 20MB, FILEGROWTH = 10%) " +
             "LOG ON (NAME = My_cool_db_log, " +
             "FILENAME = '" + @log_path + "', " +
             "SIZE = 4MB, " +
             "MAXSIZE = 5MB, " +
             "FILEGROWTH = 10%)";

             ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + @base_path + ";Integrated Security=True";
             connection = new SqlConnection(ConnectionString);
             cmnd = connection.CreateCommand();
             cmnd.CommandText = cmnd_create_db;                
             connection.Open();
             cmnd.ExecuteNonQuery();



             List<string> db_cmnd = new List<string>();
             db_cmnd.Add(File.ReadAllText(Environment.CurrentDirectory + "\\res\\Main.sql"));
             db_cmnd.Add(File.ReadAllText(Environment.CurrentDirectory + "\\res\\Category_table.sql"));
             db_cmnd.Add(File.ReadAllText(Environment.CurrentDirectory + "\\res\\ForeignKeyConstraint1.sql"));
             foreach (string command_text in db_cmnd)
             {
                 cmnd.CommandText = command_text;
                 cmnd.ExecuteNonQuery();
             }

            connection.Close();
            connection.Dispose();
            cmnd.Dispose();



    }
0

There are 0 answers