如何解决有谁知道为什么会发生这种情况?相同的代码在不同的PC上不起作用?这可能是PostgreSQL问题吗?
| 嗨,我已经在C#中使用以下代码在PostgreSQL中进行数据库备份。单击备份按钮时将运行此代码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
}
}
现在我的问题是这部分代码在我的系统中可以正常工作,并且备份已正确进行。
但是在另一个系统中不起作用,它最终出现并卡在语句中
string result = proc.StandardOutput.ReadToEnd();
有谁知道为什么会发生这种情况?相同的代码在不同的PC上不起作用?这可能是PostgreSQL问题吗
提前致谢!!!!
解决方法
\“相同的代码不能在不同的PC \'S \”工作并不奇怪,因为不同的计算机可以具有不同的环境。一方面,您正在调用系统命令
pg_dump -h \" + host + \" -U \" + userName + \" -p \" + port + \" -F c -b -v \" + dataBase
您检查过它是否可以在第二台机器上工作吗? (即pg_dump在您的路径中,您可以使用这些参数连接到pg DB)?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。