从C#Postgres数据库备份/恢复 - CodeProject [英] Postgres database backup/restore from C# - CodeProject

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

问题描述

Postgres Database Backup/Restore From C# - CodeProject





我尝试过:





What I have tried:

private void butBackup_Click(object sender, EventArgs e)
{
    try
    {
        if (textBox1.Text == "-------")
        {
            MessageBox.Show("Select the location to save");
            return;
        }
        StreamWriter sw = new StreamWriter("DBBackup.bat");
        // Do not change lines / spaces b/w words.
        StringBuilder strSB = new StringBuilder(strPG_dumpPath);

        if (strSB.Length != 0)
        {
            strSB.Append("pg_dump.exe --host " + strServer + " --port " + strPort + 
              " --username postgres --format custom --blobs --verbose --file ");
            strSB.Append("\"" + textBox1.Text + "\"");
            strSB.Append(" \"" + strDatabaseName + "\r\n\r\n");
            sw.WriteLine(strSB);
            sw.Dispose();
            sw.Close();
            Process processDB = Process.Start("DBBackup.bat");
            do
            {//dont perform anything
            }
            while (!processDB.HasExited);
            {
                MessageBox.Show(strDatabaseName + " Successfully Backed up at " + textBox1.Text);
            }
        }
        else
        {
            MessageBox.Show("Please Provide the Location to take Backup!");
        }
    }
    catch (Exception ex)
    { }
}

推荐答案

我认为您的问题是批处理文件的默认路径不是什么你期望,大多数是 \ Windows \ System32

你不需要创建一个批处理文件,一切都是c可以使用 Process.Start()完成。

我成功地使用了这个备份和恢复,但是找不到常规,也许接下来我再次上​​班的那一周:)

我还建议使用 Path.Combine()来连接像这样的路径strPG_dumpPath ,带 pg_dump.exe
I think your problem is that the default path for a batch file is not what you expect, mostly this is \Windows\System32.
You don't need to create a batch file, everything can be done using Process.Start().
I used this successfully to backup and restore, can't find the routine though, maybe next week when I'm at work again :)
I also recommend using Path.Combine() to concatenate paths like strPG_dumpPath with pg_dump.exe.


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

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