c# – 从流中读取时,Nggsql异常,Postgres

前端之家收集整理的这篇文章主要介绍了c# – 从流中读取时,Nggsql异常,Postgres前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在代码中间歇性地收到此错误.有时它会在一段时间后发生.有时它发生在10次中.我不会在我的sql中做任何独特或特殊的事情,而不像StackOverflow上正在执行COPY命令的其他海报.我所做的只是选择.

这是堆栈跟踪:

Exception while reading from stream
at Npgsql.ReadBuffer.Ensure(Int32 count,Boolean dontBreakOnTimeouts)
at Npgsql.NpgsqlConnector.DoReadMessage(DataRowLoadingMode  dataRowLoadingMode,Boolean isPrependedMessage)
at Npgsql.NpgsqlConnector.ReadMessageWithPrepended(DataRowLoadingMode dataRow LoadingMode) 
at Npgsql.NpgsqlConnector.ReadMessage(DataRowLoadingMode dataRowLoadingMode) 
at Npgsql.NpgsqlConnector.ReadExpecting[T]() 
at Npgsql.NpgsqlDataReader.NextResultInternal() 
at Npgsql.NpgsqlDataReader.NextResult() 
at Npgsql.NpgsqlCommand.Execute(CommandBehavior behavior) 
at Npgsql.NpgsqlCommand.ExecuteDbDataReaderInternal(CommandBehavior behavior) 
at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior) 
at System.Data.Common.DbCommand.ExecuteReader() 
at Npgsql.NpgsqlCommand.ExecuteReader() 
at JBetaFinder.Program.portfolioSimulation(String beginResult,String endResult) in c:\Users\j\Documents\Visual Studio 2013\Projects\JBetaFinder\JBetaFinder\Program.cs:line 571

有关如何避免此错误的任何建议?这是Npgsql和postgres的问题吗?

这是我的sql语句,似乎是最成问题的:

select leg1.trade_date,sum(p.qty) as totalqty,max(gas.net_change)*10000 as avggaschange,sum(((leg1.settlement_price - leg2.settlement_price) - (leg3.settlement_price - leg4.settlement_price))*qty*1000000) as spread_value_weight
            from quant_portfolio p
            inner join (select distinct trade_date,hub,product,strip,settlement_price,net_change
                            from public.icecleared_gas where contract = 'H') leg1
                            on p.leg1 = leg1.strip
            inner join (select distinct trade_date,net_change
                            from public.icecleared_gas where contract = 'H') leg2
                            on p.leg2 = leg2.strip and leg1.trade_date = leg2.trade_date                
            inner join (select distinct trade_date,net_change
                            from public.icecleared_gas where contract = 'H') leg3
                            on p.leg3 = leg3.strip and leg1.trade_date = leg3.trade_date                
            inner join (select distinct trade_date,net_change
                            from public.icecleared_gas where contract = 'H') leg4
                            on p.leg4 = leg4.strip and leg1.trade_date = leg4.trade_date  
            inner join (select distinct trade_date,contract,net_change
                            from public.icecleared_gas where contract = 'H') gas
                            on gas.strip = (select min(leg1) from quant_portfolio where commodity = 'NG') and gas.trade_date = leg1.trade_date                         
            where p.commodity = 'NG'
            AND (leg1.trade_date>='xxx' and leg1.trade_date<='yyy')
            group by leg1.trade_date
            order by leg1.trade_date

我尝试重新安排sql来取出子SELECTS并使它们成为所有连接;没有帮助,同样的错误.

这是调用Npgsql的C#代码

query = new NpgsqlCommand(getFullQuantPortBeta.ToString().Replace("xxx",beginResult.ToString()).Replace("yyy",endResult.ToString()),conn);
            dr = query.ExecuteReader();//code does not get past this line!
            beta = 0;
            while (dr.Read())
            {
                baselineData.Add(double.Parse(dr[2].ToString()));
                responseData.Add(double.Parse(dr[3].ToString()));
                if (baselineData.Count > 3)
                {
                    Tuple<double,double> result = MathNet.Numerics.LinearRegression.SimpleRegression.Fit(baselineData.ToArray(),responseData.ToArray());
                    beta = result.Item2 * BETA_MULT;
                    Console.WriteLine("WEIGHT BETA = " + beta);
                }
            }
            dr.Close();

解决方法

conn = new NpgsqlConnection(“Server = myserver; User Id = postgres; Password = somepw; Database = somedb; Pooling = false; Timeout = 300; CommandTimeout = 300”);

我将CommandTimeout属性添加到我的连接字符串,它似乎现在正在工作.超时错误的奇怪异常……

原文链接:https://www.f2er.com/csharp/238990.html

猜你在找的C#相关文章