从C#代码备份数据库 [英] Backing up database from C# code

查看:66
本文介绍了从C#代码备份数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个例程来备份我的数据库。它编译并运行。我没有任何异常,但备份文件上的日期不会改变,因为我说我没有得到任何反馈。我在这里需要帮助:

 private void pushBackTables_pg2_Click(object Sender,EventArgs e)
{
//从配置中读取连接字符串file
DoServer.GetSrvConnOrSwitch(" ComeAndGetThr");
using(SqlConnection conn = new SqlConnection(Globals.srv.ConnectionContext.ConnectionString))
{
//从配置文件读取备份文件夹(" C:\\VCSharp_Projects \ \ComeAndGet \\ComeAndGet \\ConfigFile \\ConfigurationFile.config")
var backupFolder = ConfigurationManager.AppSettings [" C:\\VCSharp_Projects \\ComeAndGet \\\ \\ComeAndGet\\ConfigFile\\ConfigurationFile.config"];
// set backupfilename
var backupFileName = String.Format(" ComeAndGetThr.bak");
using(var connection = new SqlConnection(Globals.srv.ConnectionContext.ConnectionString))
{
var query = String.Format(" BACKUP DATABASE [ComeAndGetThr] TO DISK ='C' ");
using(var command = new SqlCommand(query,connection))
{
connection.Open();
command.ExecuteNonQuery();
}
}
}
} // pushBackTables_pg2_Click

我得到了这段代码来自本网站的
。我发现他的代码不完整,省略了一些基本细节。我认为我的代码不起作用,没有发生备份,但为什么?


- MyCatAlex



解决方案

您的备份查询看起来不完整。它缺少一些必要的参数。


你可以检查
此链接
,其中建议使用几种方法进行数据库备份。



I have this routine to backup my database. It compiles and runs. I don't get any exception but the date on the backup file does not change and as I said I don't get any feedback. I need help here:

private void pushBackTables_pg2_Click(object Sender, EventArgs e)
        {
            // read connectionstring from config file
            DoServer.GetSrvConnOrSwitch("ComeAndGetThr");
            using (SqlConnection conn = new SqlConnection(Globals.srv.ConnectionContext.ConnectionString))
            {
                // read backup folder from config file ("C:\\VCSharp_Projects\\ComeAndGet\\ComeAndGet\\ConfigFile\\ConfigurationFile.config")
                var backupFolder = ConfigurationManager.AppSettings["C:\\VCSharp_Projects\\ComeAndGet\\ComeAndGet\\ConfigFile\\ConfigurationFile.config"];
                // set backupfilename 
                var backupFileName = String.Format("ComeAndGetThr.bak");
                using (var connection = new SqlConnection(Globals.srv.ConnectionContext.ConnectionString))
                {
                    var query = String.Format("BACKUP DATABASE [ComeAndGetThr] TO DISK='C'");
                    using (var command = new SqlCommand(query, connection))
                    {
                        connection.Open();
                        command.ExecuteNonQuery();
                    }
                }
            }
        }                         // pushBackTables_pg2_Click

I got this code from this website. I found his code incomplete with some essential details omitted. I think my code does not work and no backup happened, but why?

- MyCatAlex

解决方案

It looks like your backup query is incomplete. It is missing some required param.

Can you check this link where couple of approaches are suggested for db backup.


这篇关于从C#代码备份数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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