且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从C#代码备份数据库

更新时间:2023-02-12 23:35:36

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


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



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.