如何使用PostgreSql 9.4和C#创建还原?我正在使用这个方法,但是当我运行这个过程时它不起作用!怎么了?
private void btnRestore_Click(object sender, EventArgs e)
{
if (clsB.ConectaBanco())
{
//Executo a seguinte função para limpar a base de dados, para poder dar o restore.
clsB.ExecutarSQL("drop schema public cascade; create schema public;");
//E executo o seguinte processo
string Comando = CaminhoPg + @"psql -U postgres -d restore2 -f C:\Users\bruhh\Desktop\Backup\back.backup";
Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = CaminhoPg + @"psql" ;
p.StartInfo.Arguments = @"-U postgres -d restore2 -f C:\Users\bruhh\Desktop\Backup\back.backup";
p.Start();
p.WaitForExit();
p.Close();
MessageBox.Show(Comando);
}
else
MessageBox.Show("There was an error loading database settings");
}
各种Comando的结果是
C:\\Program Files\\PostgreSQL\\9.4\\bin\\psql -U postgres -d restore2 -f C:\\Users\\bruhh\\Desktop\\Backup\\back.backup
该过程甚至可以在所有行中执行"无效命令"出现。如何使这段代码有效?
进程运行时如何获得 https://i.stack.imgur.com/2AUvL.png
用于执行备份的方法
public string BackupDatabase(string CaminhoNome)
{
string server = clsConfigBanco.SERVERNAME;
string port = clsConfigBanco.PORT;
string user = clsConfigBanco.USERNAME;
string password = clsConfigBanco.PASSWORD;
string dbname = clsConfigBanco.DATABASENAME;
string backupCommandDir = @"C:\Program Files\PostgreSQL\9.4\bin";
try
{
Environment.SetEnvironmentVariable("PGPASSWORD", password);
string backupFile = CaminhoNome;
string BackupString = "-ibv -Z3 -f \"" + backupFile + "\" " +
"-Fc -h " + server + " -U " + user + " -p " + port + " " + dbname;
Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = CaminhoPg + "\\pg_dump.exe";
p.StartInfo.Arguments = BackupString;
p.Start();
p.WaitForExit();
p.Close();
return backupFile;
}
catch
{
return "";
}
}
显然备份是正确的,因为当我生成备份时,我可以通过PostgreSql9.4进行恢复,但我无法使用该应用程序。我的备份文件也有一些奇怪的字符,这是正常的吗? Here is a system-generated backup
如何让恢复工作?