我有一个 SQL Server 数据库和该数据库的旧备份。有时我必须使用 C# WinForms 应用程序验证备份副本中的一些数据。我使用这篇文章恢复备份文件:如何在 sql server 中恢复到不同的数据库?.
我的恢复功能如下所示:
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(); }
}
和
database.ReadDataBaseIstoric(dataGridview1,Path.GetFileName(filename));
从恢复的数据库中读取数据,如下所示:
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 && !reader.IsClosed) { reader.Close(); }
if (conn.State == ConnectionState.Open) { conn.Close(); }
}
}
目前一切正常。问题是,当我尝试删除恢复的数据库时,它返回 en 错误,表示无法删除数据库,因为它仍在使用中。这就是我要删除数据库的方式:
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(); }
}
我曾尝试在从中读取数据之前删除恢复的数据库,并且效果很好,所以我认为问题应该出在database.ReadDataBaseIstoric()
.
PS我database.ReadDataBaseIstoric()
也使用该功能从当前数据库(不是恢复的备份)读取数据,它工作得很好,没有任何错误或异常。