如何备份或复制和还原或加载.Mdf附加的数据库文件

时间:2014-01-10 12:24:09

标签: c# sql-server

我使用SQL Server Express LocalDB,并且我有一个.mdf数据库文件,可以自由浮动附加数据库。

我有一个带按钮的表单,当按下按钮时,SaveFileDialog被打开。

我想在用户选择的地方保存.mdf数据库,然后我有另一个表单,其中包含要恢复的按钮,或者在此.mdf数据库中复制并覆盖现有数据库。

private void button1_Click(object sender, EventArgs e)
{
   ((( connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Expert-Pro-SoftDataBase.mdf;Integrated Security=True"))) this is the connection string

    saveFileDialog1.ShowDialog();            

    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Expert"].ToString());

    if (con.State.ToString() == "Closed")
    {
       con.Open();
    }

    SqlCommand com = new SqlCommand("Backup Database @E TO DISK = @disk", con);
    com.Parameters.Add(new SqlParameter("@E", "DatabaseTest.mdf"));(Another question is how can I find out the path of the Mdf. file when installs on another machine?)
    com.Parameters.Add(new SqlParameter("@disk", saveFileDialog1.FileName));
    com.ExecuteNonQuery();

    MessageBox.Show("Datele au fost salvate", "Expert Pro-Soft Facturare", MessageBoxButtons.OK, MessageBoxIcon.Information);
    con.Close();            
}   

我听说这个.mdf附加的自由浮动无法备份或恢复,只能被复制。然后我想恢复它并用已保存的当前.mdf覆盖它。谢谢大家。

因为它在磁盘上我想复制它,但你是怎么做的,然后用当前的那个替换复制的那个。要复制我有一个按钮,并重新加载或恢复复制的.mdf文件,我有另一个按钮

0 个答案:

没有答案
相关问题