I'm having trouble executing the following code :
Code: Select all
private void testFunc()
{
string sql = @"SELECT * FROM
empresa c LEFT OUTER JOIN
telefone_flat tf ON c.emp_id = tf.emp_id
WHERE
emp_excluida = 0 AND
(emp_razao_social like :ptest OR
emp_fantasia like :ptest ) AND
(emp_tipo & 1) = 1
ORDER BY
emp_razao_social";
// connect to postgresql
using (IDbConnection conn = Connect())
{
// create command object
IDbCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
// create parameter object
PgSqlParameter psearch = new PgSqlParameter();
psearch.ParameterName = "ptest";
psearch.Value = "%A%";
psearch.DbType = DbType.String;
// add parameter to command
cmd.Parameters.Add(psearch);
// open reader
using (IDataReader r = cmd.ExecuteReader())
{
// some processing code here...
}
}
}
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[SocketException (0x274c): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond]
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) +1018043
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) +119
[PgSqlException (0x80004005): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond]
CoreLab.PostgreSql.PgSqlCommand.a(CommandBehavior A_0, IDisposable A_1, Int32 A_2, Int32 A_3) +387
CoreLab.Common.DbCommandBase.c(CommandBehavior A_0) +129
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
After investigating the possible causes of the error, I found out that if I do one of the things listed below, the query executes OK :
1 - Remove the bitwise and operator in "(emp_tipo & 1) = 1" and make the clause "(emp_tipo = 1)".
2 - Use a more restrictive value for the "ptest" parameter, say "%TEST%", which reduces the returned rows from about 6000 to less than 200.
3 - Remove the join for the "telefone_flat" view, which is simply a view that selects data from a single table using the "DISTINCT ON" feature of PostgreSQL.
4 - Change the code to avoid using the parameter object and replace the parameter value directly in the sql string.
I'm using the latest version of PostgreSQLDirect (2.5.21).
Thanks in advance.