Note that there are some explanatory texts on larger screens.

plurals
  1. POSql Server Drop Database failure
    primarykey
    data
    text
    <p>I have a SQL Server database and an old backup of that database. Sometimes I have to verify some data in the backup copy using a C# WinForms application. I restore the backup file using this post: <a href="https://stackoverflow.com/questions/6267273/how-to-restore-to-a-different-database-in-sql-server">How to restore to a different database in sql server?</a>.</p> <p>My restore function looks like this:</p> <pre><code>SqlConnection myConn = new SqlConnection("Server=.\\sqlexpress;Database=master;Trusted_Connection=True;"); try { if (!Databases.CheckDatabaseExists(myConn, fileName)) { myConn.Open(); SqlCommand cmd = new SqlCommand("RESTORE FILELISTONLY FROM DISK='" + fileName + ".bak'", myConn); SqlDataReader reader = cmd.ExecuteReader(); cmd.CommandText = "restore database " + Path.GetFileName(fileName) + " from disk = '" + fileName + ".bak' with move'"; int i = 0; while (reader.Read()) { if (i == 0) { cmd.CommandText += reader[0].ToString() + "' to '" + filePath + "\\" + Path.GetFileName(fileName) + ".mdf', move "; i++; } else { cmd.CommandText += "'" + reader[0].ToString() + "' to '" + filePath + "\\" + Path.GetFileName(fileName) + ".mdf.ldf'"; } } reader.Close(); cmd.ExecuteNonQuery(); myConn.Close(); database.ReadDataBaseIstoric(dataGridView1, Path.GetFileName(fileName)); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (myConn.State == ConnectionState.Open) { myConn.Close(); } } </code></pre> <p>and the </p> <pre><code>database.ReadDataBaseIstoric(dataGridview1,Path.GetFileName(filename)); </code></pre> <p>reads the data from the restored database and looks like this:</p> <pre><code>public void ReadDataBaseIstoric(DataGridView dataGridView1, string dataBaseName) { dataGridView1.Rows.Clear(); SqlConnection conn = new SqlConnection("Server=.\\sqlexpress;Trusted_Connection=true;database=" + dataBaseName + ";"); SqlDataReader reader = null; try { conn.Open(); SqlCommand cmd = new SqlCommand("select * from istoric", conn); conn.Close(); reader = cmd.ExecuteReader(); while (reader.Read()) { string[] str = new string[5] { reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString() }; dataGridView1.Rows.Add(str); } reader.Close(); conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (reader != null &amp;&amp; !reader.IsClosed) { reader.Close(); } if (conn.State == ConnectionState.Open) { conn.Close(); } } } </code></pre> <p>Everything works fine for now. The problem is that when I try to delete the restored database it returns en error which says that database couldn't be deleted because it's still in use. This is how I want to delete the database:</p> <pre><code>private void Arhiva_FormClosing(object sender, FormClosingEventArgs e) { bool closed = false; if (!closing) { e.Cancel = true; closing = false; closed = true; } SqlConnection myConn = new SqlConnection("Server=.\\sqlexpress;Database=master;Trusted_Connection=True;"); try { if (Databases.CheckDatabaseExists(myConn, Path.GetFileName(fileName))) { myConn.Open(); SqlCommand cmd = new SqlCommand("DROP DATABASE "+Path.GetFileName(fileName), myConn); cmd.ExecuteNonQuery(); myConn.Close(); label1.Visible = false; } else { MessageBox.Show("Exista deja o baza de date cu numele '" + fileName + "'.", "VivaFEED", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (myConn.State == ConnectionState.Open) { myConn.Close(); } } if (closed) { this.Close(); } } </code></pre> <p>I have tried to delete the restored database before reading the data from it and it worked great, so I think the problem should be in the <code>database.ReadDataBaseIstoric()</code>.</p> <p>P.S. I am using the <code>database.ReadDataBaseIstoric()</code> function also for reading data from the current database (not the restored backup) an there it works great, without any errors or exceptions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload