如何备份我的MDF文件 [英] how to back up my MDF file

查看:143
本文介绍了如何备份我的MDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All



我已经生成了一个Windows应用程序。我使用过.MDF文件。

该项目工作正常但我想在点击按钮时备份我的.Mdf文件。当我从bin文件夹tuser位置复制此文件时,它会出现错误

喜欢'进程无法访问文件'.mdf',因为它正被另一个进程使用'。





我应该使用什么代码/技术我应该使用,所以我可以这样做。





我正在使用:



Visual Studio 2010,

dot net framwork 3.5 sp1

Microsoft Sql server 2005的MDF文件。



这是编码

Hello All

I have generated an windows application. I have used .MDF file.
The project is working fine but I want to backup my .Mdf file on a button click. When i copy this file from bin folder tuser location then it gives an error
like 'The process cannot access the file '.mdf' because it is being used by another process'.


What code should i use/ technique should i use so I can do so.


I am using :

Visual Studio 2010,
dot net framwork 3.5 sp1
MDF FILE of Microsoft Sql server 2005.

Here is the coding

con="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Test.mdf;Integrated Security=True;User Instance=True"

 SaveFileDialog SaveFD1 = new SaveFileDialog();
 string FileName = "";
 SaveFD1.FileName = "Test";
 SaveFD1.Title = "Backup ";
 SaveFD1.Filter = "Database files (*.mdf)|*.mdf";
 SaveFD1.FilterIndex = 1;
 SaveFD1.DefaultExt = "mdf";
 SaveFD1.RestoreDirectory = true;
 if (SaveFD1.ShowDialog() == DialogResult.OK)
  {                    
    SqlConnection.ClearAllPools();
    Ut.con.Close();
    FileName = SaveFD1.FileName
    string sourcePath = Application.StartupPath;
    string src = sourcePath + "\\Test.mdf";
    string dst = path;                   
    System.IO.File.Copy(src, dst, true);
 }

推荐答案

这是解决方案。我用Google搜索了很多天..

Here is the solution. I found it from many days googled..
try
{
    SaveFileDialog sd = new SaveFileDialog();
    sd.Filter = "SQL Server database backup files|*.bak";
    sd.Title = "Create Database Backup";

    if (sd.ShowDialog() == DialogResult.OK)
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["project_name.Properties.Settings.project‌​_nameConnectionString"].ConnectionString))
        {
            string sqlStmt = string.Format("backup database [" + System.Windows.Forms.Application.StartupPath + "\\dbname.mdf] to disk='{0}'",sd.FileName);
            using (SqlCommand bu2 = new SqlCommand(sqlStmt, conn))
            {
                conn.Open();
                bu2.ExecuteNonQuery();
                conn.Close();

                MessageBox.Show("Backup Created Sucessfully");
            }                   
        }
    }
}
catch (Exception)
{
    MessageBox.Show("Backup Not Created");
}


您不通过复制活动的MDF文件(或任何其他数据库文件)进行备份。由于缓冲机制,数据可能处于不一致状态。



相反,使用备份 [ ^ ]语句在某处获得一致的备份,然后将该文件复制到安全的位置。
You don't take a backup by copying an active MDF file (or any other database file). Because of the buffering mechanisms the data may be in inconsistent state.

Instead, use the BACKUP[^] statement to get a consistent backup somewhere and then copy that file to a safe location.


这篇关于如何备份我的MDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆