简体   繁体   English

如何使用C#将mdf文件复制到文件夹中

[英]How to copy mdf file into a folder with c#

I'm using c# and I have a small SQL Server database that I need to copy into a folder C:\\databases\\ , the method name is CreateCopy . 我正在使用c#,并且我有一个小型SQL Server数据库,需要将其复制到文件夹C:\\databases\\ ,方法名称为CreateCopy

But at File.Copy row appear an error: "The Process cannot Access the File, because it is being use by another Process" 但是在File.Copy行上出现错误: "The Process cannot Access the File, because it is being use by another Process"

I read that File.Copy can be execute only after shut SqlServer down or Detach that database, create a copy and turn SqlServer on again. 我读到File.Copy仅在关闭SqlServer或分离该数据库,创建副本并再次打开SqlServer后才能执行。 But how to do it by code? 但是如何通过代码实现呢?

This is the method that I was trying to use: 这是我尝试使用的方法:

public static void CreateCopy()
{
    try
    {
        DateTime date = DateTime.Now;
        SqlConnection connection = new SqlConnection(MDF_CONNECTION_STRING);

        String dbpath = String.Format(@"C:\databases\{0:yyyyMMdd}.mdf", Cash, date);
        File.Copy(@"C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\database.mdf", dbpath);
        String lgpath = String.Format(@"C:\databases\{0:yyyyMMdd}_log.ldf", Cash, date);
        File.Copy(@"C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\database_log.ldf", lgpath);    
    }
    catch(Exception e)
    {
        throw new ApplicationException("Error", e);
    }
}

Thanks in advance! 提前致谢!

INFO This is not a duplicate of here because i don't need to do create a bak file, i need to archive this database. 信息这不是这里的重复项,因为我不需要创建bak文件,我需要存档此数据库。 I need just to copy these two files (.mdf and .ldf) into a folder. 我只需要将这两个文件(.mdf和.ldf)复制到一个文件夹中即可。 And that answers didn't help me 那答案对我没有帮助

What I understand is that you are looking for the code which will help you to Programmatically Enumerating, Attaching, and Detaching SQL Server Databases So that you can copy the MDF file to the location. 据了解,您正在寻找的代码将帮助您以编程方式枚举,附加和分离SQL Server数据库 ,以便可以将MDF文件复制到该位置。 You can also have a look on How to Backup and Restore to get the idea of AttachDbFilename mode. 您也可以查看如何备份和还原,以了解AttachDbFilename模式。

Database files .mdf and .ldf files are used by the SQL Server engine. SQL Server引擎使用数据库文件.mdf和.ldf文件。

If you DETACH database from SQL Server instance, then you can copy or move those files. 如果从SQL Server实例中分离数据库,则可以复制或移动这些文件。

But when you DETACH db, it will be unaccessible! 但是,当您分离数据库时,将无法访问它!

So it is better to run a backup command in SQL then use it. 因此,最好在SQL中运行备份命令然后再使用它。

Remove SqlConnection connection = new SqlConnection(MDF_CONNECTION_STRING); 删除SqlConnection connection = new SqlConnection(MDF_CONNECTION_STRING); it will access the .mdf file and give it after the File.Copy() execute. 它会访问.mdf文件,并在File.Copy()执行后给出该文件。 because at the time of execution of File.Copy() the file is being used by SqlConnection that's because you are getting such error 因为在执行File.Copy()SqlConnection正在使用该文件,这是因为您收到这样的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM