Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅当我将| DataDirectory |连接字符串放入app.config中时,数据库备份或还原才起作用_C#_Sql Server_Database_Database Backups - Fatal编程技术网

C# 仅当我将| DataDirectory |连接字符串放入app.config中时,数据库备份或还原才起作用

C# 仅当我将| DataDirectory |连接字符串放入app.config中时,数据库备份或还原才起作用,c#,sql-server,database,database-backups,C#,Sql Server,Database,Database Backups,我正在使用visual studio 2012服务库数据库。我认为它是备份,因为我看到了一个.BAK文件,但当我恢复时,它不起作用。两者在运行时都没有错误,但我认为我的代码有问题。这是我的密码: private void btnBackUp_Click(object sender, EventArgs e) { try { progressBar1.Visible = true; pro

我正在使用visual studio 2012服务库数据库。我认为它是备份,因为我看到了一个.BAK文件,但当我恢复时,它不起作用。两者在运行时都没有错误,但我认为我的代码有问题。这是我的密码:

private void btnBackUp_Click(object sender, EventArgs e)           
    {
        try
        {
            progressBar1.Visible = true;
            progressBar1.Value = 15;
            bool bBackUpStatus = true;

            Cursor.Current = Cursors.WaitCursor;

            if (Directory.Exists(@"D:\Backup_MAConvent"))
            {
                if (File.Exists(@"D:\Backup_MAConvent\MAConvent_Backup.bak"))
                {
                    if (MessageBox.Show(@"Do you want to replace it?", "Back", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        File.Delete(@"D:\Backup_MAConvent\MAConvent_Backup.bak");
                    }
                    else
                        bBackUpStatus = false;
                }
            }
            else
                Directory.CreateDirectory(@"D:\Backup_MAConvent");

            if (bBackUpStatus)
            {


                con.Open();

                progressBar1.Value = 25;

                string path1 = System.IO.Path.GetFullPath(@"SchoolDatabase.mdf");

                SqlCommand cmd2 = new SqlCommand("backup database [" + path1 + @"] to disk ='D:\Backup_MAConvent\MAConvent_Backup.bak' with init,stats=10", con);
                cmd2.ExecuteNonQuery();

                progressBar1.Value = 35;
                con.Close();

                timer1.Start();

                MessageBox.Show("Backup of the Database saved Successfully", "Back", MessageBoxButtons.OK, MessageBoxIcon.Information);
                timer1.Stop();
                progressBar1.Value = 10;
                progressBar1.Visible = false;
            }
        }
        catch
        {
            MessageBox.Show(@"Backup Error, Please close the software & restart and then try again to backup", "Backup", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

    private void btnRestore_Click(object sender, EventArgs e)
    {
        Cursor.Current = Cursors.WaitCursor;
        progressBar1.Visible = true;
        progressBar1.Value = 15;

        try
        {
            if (File.Exists(@"D:\Backup_MAConvent\MAConvent_Backup.bak"))
            {
                if (MessageBox.Show("Are you sure you restore?", "Back", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    con.Open();
                    progressBar1.Value = 25;
                    SqlCommand cmd;
                    cmd = new SqlCommand("use master", con);
                    cmd.ExecuteNonQuery();
                    progressBar1.Value = 35;
                    string path1 = System.IO.Path.GetFullPath(@"SchoolDatabase.mdf");
                    cmd = new SqlCommand("restore database [" + path1 + @"] from disk = 'D:\Backup_MAConvent\MAConvent_Backup.bak'", con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    timer1.Start();
                    MessageBox.Show("Database has been Restored", "Restoration", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    timer1.Stop();
                    progressBar1.Value = 10;
                    progressBar1.Visible = false;
                }
            }
            else
            {
                MessageBox.Show(@"This is not in the correct path, Please close the software & restart and then try again to restore", "Restoration", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
        catch (Exception exp)
        {
            MessageBox.Show(exp.Message);
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        try
        {
            progressBar1.Value = 50;
            progressBar1.PerformStep();
            if (progressBar1.Value <= progressBar1.Maximum)
                timer1.Stop();

            progressBar1.Value = progressBar1.Maximum;
        }
        catch
        {
        }
    }
但我不想使用| DataDirectory |字符串。请建议在备份和还原代码中更改什么?请帮忙
提前谢谢

我自己找到了答案。如果将来有人遇到同样的问题,我在这里提供答案。 值得注意的是,一个项目中有两个数据库,即一个数据库是主应用程序目录数据库,另一个是输出数据库。当我们开始调试时,主数据库将所有表及其数据复制到输出目录数据库中,通常
\bin\Debug\database.mdf
。因此,如果我们在连接字符串中使用
| DataDirectory |
,备份会很好地工作,但这不是一个好方法,因为通过这样做,我们将所有数据恢复到\bin数据库中,仅用于输出。因此,我们应提供如下完整的连接字符串:

<connectionStrings>
    <add name="SchoolManagement.Properties.Settings.SchoolDatabaseConnectionString"
        connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=G:\Users\ABC\documents\visual studio 2012\Projects\SchoolManagement\SchoolManagement\SchoolDatabase.mdf;Integrated Security=True;User Instance=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SchoolDatabase.mdf;Integrated Security=True;User Instance=True"
<connectionStrings>
    <add name="SchoolManagement.Properties.Settings.SchoolDatabaseConnectionString"
        connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=G:\Users\ABC\documents\visual studio 2012\Projects\SchoolManagement\SchoolManagement\SchoolDatabase.mdf;Integrated Security=True;User Instance=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>
string path1 = System.IO.Path.GetFullPath(@"G:\Users\ABC\documents\visual studio 2012\Projects\SchoolManagement\SchoolManagement\SchoolDatabase.mdf");
SqlCommand cmd2 = new SqlCommand("backup database [" + path1 + @"] to disk ='D:\Backup_MAConvent\MAConvent_Backup.bak' with init,stats=10", con);
cmd2.ExecuteNonQuery();