How to make a progress bar for PostgreSQL DB backup through c#?

Viewed 1613

I am working on a c# winforms program, where the data is saved to a database. Now transferring the DB from MSSQL to PostgreSQL.

I am looking for a way to backup (and restore) through the program, with a progress bar showing the backup status.

The only backup option I found is using pg_dump through a cmd.exe method (see below code), but this does not provide the feedback of success/failure, progress percentage, etc.

Is there a way to backup and provide progress reports?

Thank you

            ProcessStartInfo startInfo = new ProcessStartInfo()
            {
                FileName = "cmd.exe", 
                UseShellExecute = false,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true
            };

            Process backupProcess = new Process();
            backupProcess.StartInfo = startInfo;
            backupProcess.StartInfo.EnvironmentVariables["PGPASSWORD"] = "xxxxxxx";

            backupProcess.Start();

            using (StreamWriter sw = backupProcess.StandardInput)
            {
                if (sw.BaseStream.CanWrite)
                {
                    command = @"cd C:\Program Files\PostgreSQL\12\bin"; 
                    sw.WriteLine(command); 
                    command = @"pg_dump -U postgres -d " + databaseName.ToLower() + " -f " + fileName;
                    sw.WriteLine(command);
                }
            }
0 Answers
Related