Hi I’ve used the below code in C# to take the backup of a database in PostgreSQL.This code runs when the backup button is clicked
private void lnkSCBackup_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "Postgre backups (*.backup)|*.backup";
save.ShowDialog();
if (save.FileName != "")
{
string saveFileName = "\"" + save.FileName + "\"";
string host = S ystem.Configuration.ConfigurationSettings.AppSettings["HOST"].ToString().Trim();
string port = System.Configuration.ConfigurationSettings.AppSettings["PORT"].ToString().Trim();
string userName = System.Configuration.ConfigurationSettings.AppSettings["USERNAME"].ToString().Trim();
string password = System.Configuration.ConfigurationSettings.AppSettings["PASSWORD"].ToString().Trim();
string dataBase = System.Configuration.ConfigurationSettings.AppSettings["DATABASE"].ToString().Trim();
try
{
string Creten = "pg_dump -h " + host + " -U " + userName + " -p " + port + " -F c -b -v " + dataBase + " > " + saveFileName;
try
{
// create the ProcessStartInfo using "cmd" as the program to be run,
// and "/c " as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command that follows,
// and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + Creten);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
//Gets the total processing time for this particular process
string totalProcessingTime = proc.TotalProcessorTime.Ticks.ToString();
//progress bar working
progressBar1.Visible = true;
progressBar1.BringToFront();
progressBar1.Minimum = 0;
progressBar1.Maximum = int.Parse(totalProcessingTime);
progressBar1.Step = 500;
progressBar1.Value = 0;
this.lblBackup.Visible = true;
this.lblBackup.Text = "Backing Up Database";//To show the user it is backing up
this.lblBackup.Font = new Font("Microsoft Sans Serif", 9, System.Drawing.FontStyle.Regular);
this.Refresh();
while (progressBar1.Value < progressBar1.Maximum)
{
progressBar1.Value += 10;//= 10000;
}
progressBar1.Visible = false;
this.lblBackup.Visible = false;
int exitCode = proc.ExitCode;
if (exitCode.Equals(0))
{
MessageBox.Show(" Backup Success ");
}
else
{
MessageBox.Show(" Backup not Success ");
}
}
catch (Exception objException)
{
// Log the exception
}
}
Now my problem is this part of code works fine in my system and the backup is taken properly.
But does not work in another system it finally comes and gets stuck in statement
string result = proc.StandardOutput.ReadToEnd();
Does anyone have an idea why this could happen??The same code not working in different PC’S??? Could this be a PostgreSQL issue
Thanks in advance!!!!
It’s not all strange that “The same code not working in different PC’S”, as different machines can have different environments. For one thing, your are invoking the system command
Have you checked that it works in the second machine? (i.e. pg_dump is in your path AND you can connect to your pg DB with those parameters)?