程序在psql删除语句后卡住

时间:2015-02-19 07:39:37

标签: c# postgresql ssh sql-delete

我的程序与ssh(ssh.net)连接到主机,数据提供程序npgsql连接到psql数据库并删除具有给定ID的条目。

它会删除条目,但程序会在此之后卡住至少1分钟。使用使用语句,连接应该在我认为的sql-command之后断开连接,但是我也没有收到错误,为什么它需要这么长时间。

有什么我忘记了,不得不等待超时吗?

SshClient client = new SshClient(host, username, password);

try { //SSH Connection client.Connect(); if (client.IsConnected) { scrLog.Content += "SSH connection successfull"; scrLog.Content += Environment.NewLine; } else { scrLog.Content += "SSH connection failed"; scrLog.Content += Environment.NewLine; }

//Portforwarding to Port 5432 (postgresql) var portFwdL = new ForwardedPortLocal(psqlBoundHost, psqlUPort, psqlHost, psqlUPort); client.AddForwardedPort(portFwdL); portFwdL.Start(); if (portFwdL.IsStarted) { scrLog.Content += "Portforwarding started"; scrLog.Content += Environment.NewLine; } else { scrLog.Content += "Portforwarding failed"; scrLog.Content += Environment.NewLine; } //Connection to Database with a Delete Command try { string connstring = String.Format("Server={0};Port={1};" + "User Id={2};Password={3};Database={4}", psqlBoundHost, psqlPort, sqluser, sqlpass, database); using (NpgsqlConnection conn = new NpgsqlConnection(connstring)) { conn.Open(); string sql1 = "delete from tblTest where testId = '" + nr + "';"; //string sql2 = "\q"; NpgsqlCommand cmd1 = new NpgsqlCommand(sql1, conn); cmd1.ExecuteNonQuery(); scrLog.Content += sql1; scrLog.Content += Environment.NewLine; //NpgsqlCommand cmd2 = new NpgsqlCommand(sql2, conn); //cmd2.ExecuteNonQuery(); //scrLog.Content += sql2; //scrLog.Content += Environment.NewLine; } } catch (Exception ex) { MessageBox.Show("Error with the psql-connection:" + Environment.NewLine + ex.Message); } client.Disconnect(); scrLog.Content += "SSH connection closed"; scrLog.Content += Environment.NewLine; } catch (Exception ex) { MessageBox.Show("Error with the ssh-connection:" + Environment.NewLine + ex.Message); }

我尝试使用

  

\ q

命令最后,但它总是给我错误" 42601:语法错误在或附近" \""我读到它并不需要。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

在研究和测试不同场景后,我终于找到了超时。我需要禁用Connection Pooling,如果再次需要,Connection在关闭后仍然打开。 我刚用Pooling=false禁用了它。

string sqlpool = "false";
string sqlconn = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};Pooling={5};",
                    sqlserver, sqlport, sqluserid, sqlpass, sqldatabase, sqlpool);
相关问题