如何以编程方式备份​​SQL Server 2014 Express Localdb(.mdf)文件 [英] How to backup a SQL Server 2014 Express Localdb (.mdf) file programmatically

查看:60
本文介绍了如何以编程方式备份​​SQL Server 2014 Express Localdb(.mdf)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Windows应用程序,该应用程序使用SQL Server 2014 LocalDB( .mdf 文件).

I have simple Windows application which uses SQL Server 2014 LocalDB (.mdf file).

我希望每当用户单击退出按钮时,我的应用程序都会自动将其localdb文件( .mdf )备份到用户同一台计算机上的另一个文件夹中.

And I want that whenever users click exit button, my application automatically backup its localdb file (.mdf) to another folder in the same computer of users.

我在下面编写了简单的代码,但是发生了SQLException语法错误:

I wrote below simple code but a SQLException syntax error occurred:

''C:\ greendb_angelheart.mdf'附近的语法不正确

Incorrect syntax near`'C:\greendb_angelheart.mdf'

( DATABASE" {0}" 语法似乎不错)

我担心使用正常的 SqlConnection 代码连接到特定的 localdb 文件是否正确.

And I'm worried whether it's right to connect to the specific localdb file by using normal SqlConnection code.

我的简单代码是:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (MessageBox.Show("Really want to exit? Thank you !", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
    {
            e.Cancel = true;
    }
    else
    {
            string backuppath_basic = @"c:\Green_Backup";

            if (!System.IO.Directory.Exists("backuppath_basic"))
            {
                System.IO.Directory.CreateDirectory(backuppath_basic);
            }

            var greendbfileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), string.Format("greendb_{0}.mdf", personID));
            var copied_greendbfileName = string.Format(@"C:\greendb_{0}.mdf", personID);

            string localConnectionString = string.Format(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename= " + Environment.GetEnvironmentVariable("APPDATA") + @"\greendb_{0}.mdf;Integrated Security=True;Connect Timeout=30;", personID);

            SqlConnection backupConn = new SqlConnection();
            backupConn.ConnectionString = localConnectionString;
            backupConn.Open();

            SqlCommand backupcomm = backupConn.CreateCommand();
            string backupdb = @"BACKUP DATABASE ""{0}"" TO DISK '{1}'";
            backupdb = string.Format(backupdb, greendbfileName, copied_greendbfileName);

            SqlCommand backupcreatecomm = new SqlCommand(backupdb, backupConn);
            backupcreatecomm.ExecuteNonQuery();

            backupConn.Close();

            Environment.Exit(0);
        }
    }

推荐答案

经过多次试验和分析,我终于解决了.对于正在寻找解决方案的人,我的分享如下.

I finally solved after many trial and analysis. For someone who is looking for solutions, I share mine as below.

使用MS SQL Localdb进行开发的人数似乎少于其他数据库.

It seems there's fewer people are developing with MS SQL Localdb than other databases.

数据库名称不必包含.mdf之类的扩展名,并且等号=必须与DISK =一起使用

The name of database doesn't have to include extension like .mdf and the equal sign= has to be together as DISK=

string backupdb = string.Format(@"BACKUP DATABASE greendb_ {0} TO DISK ='c:\ Green_Backup \ greendb_ {0} .bak'",personID);

string backupdb = string.Format(@"BACKUP DATABASE greendb_{0} TO DISK='c:\Green_Backup\greendb_{0}.bak'", personID);

这篇关于如何以编程方式备份​​SQL Server 2014 Express Localdb(.mdf)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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