C#SQL插入Netezza。Netezza未发送返回,c代码超时
我真的很想在这方面得到一些建议,因为我已经遇到了这个问题相当多了。我有两个应用程序,大的和小的,我需要做一些与Netezza的工作。不幸的是,.net和Netezza似乎有一个共同的问题,Netezza接受一个sql命令,执行它(我在日志中确认了它),但周期性地不发送返回,我的C#app中的OLEDB连接就停在那里并超时。在Netezza日志中,我还可以看到我的会话处于打开状态,因为我的应用程序仍在等待NZ发回一些东西。这似乎只在执行多个命令的连接中发生 不管怎样,下面是一些代码,我想要一些关于如何缓解这个问题的建议。我目前正在重试计数,但我真的想要一些可能更安全的东西。对于如何处理这样一个您可能无法收到回报的问题,有人有什么建议吗 这个循环大约有135个记录更新,通常只需要1分钟。没有任何具体记录表明没有退货。它是完全随机的,在其他应用程序中也会发生 任何建议都将不胜感激!谢谢大家!C#SQL插入Netezza。Netezza未发送返回,c代码超时,c#,sql,.net,database,netezza,C#,Sql,.net,Database,Netezza,我真的很想在这方面得到一些建议,因为我已经遇到了这个问题相当多了。我有两个应用程序,大的和小的,我需要做一些与Netezza的工作。不幸的是,.net和Netezza似乎有一个共同的问题,Netezza接受一个sql命令,执行它(我在日志中确认了它),但周期性地不发送返回,我的C#app中的OLEDB连接就停在那里并超时。在Netezza日志中,我还可以看到我的会话处于打开状态,因为我的应用程序仍在等待NZ发回一些东西。这似乎只在执行多个命令的连接中发生 不管怎样,下面是一些代码,我想要一些关于
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.IO;
namespace RemoveVoidedInvoices
{
class UpdateNetezza
{
public bool NetezzaWorkFailure = false;
private void NetezzaWorkFailed()
{
NetezzaWorkFailure = true;
}
public void updateCounts(List<RecordCounts> recordCounts)
{
string connString = string.Format("Provider=NZOLEDB;Data Source={0};Initial Catalog=EBIDW;User ID=MYUSERNAME;Password={1}", Environment, passWord);
OleDbConnection netezzaConn = null;
//Due to timeout issues I am making a quick timespan entry so that I can keep track in the log of how long each day the bulk update took
DateTime Prequery = DateTime.Now;
int retrycount = 0;
try
{
netezzaConn = new OleDbConnection(connString);
netezzaConn.Open();
for (int i = 0; i < recordCounts.Count; i++)
{
try
{
if (recordCounts[i].RecordCount.ToString() != recordCounts[i].OrigCount.ToString())
{
string updateStatement = string.Format("UPDATE fct_ourtable SET LINESWRITTENTOFILE = {0} where EXTRACTFILENAME = '{1}' and LINESWRITTENTOFILE = {2}", recordCounts[i].RecordCount.ToString(), recordCounts[i].FileName, recordCounts[i].OrigCount.ToString());
Console.WriteLine("Executing query : " + updateStatement);
Console.WriteLine("Query start-time - " + DateTime.Now.ToString());
OleDbCommand exe = new OleDbCommand(updateStatement, netezzaConn);
exe.CommandTimeout = 2000;
int rowsUpdated;
rowsUpdated = exe.ExecuteNonQuery();
Console.WriteLine("Rows Updated = " + rowsUpdated.ToString());
Console.WriteLine("Query end-time - " + DateTime.Now.ToString());
Console.WriteLine();
}
else
{
Console.WriteLine("No records were removed from the file : " + recordCounts[i].FileName + ". Not updating Netezza.");
Console.WriteLine();
}
}
catch (OleDbException oledbex)
{
retrycount++;
if (retrycount > 3)
{
Console.WriteLine("Maximum number of retrys met. Canceling now.");
throw new System.Exception();
}
else
{
i = i - 1;
Console.WriteLine("Timeout on Query, retrying");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
NetezzaWorkFailed();
}
finally
{
if (netezzaConn != null)
{
netezzaConn.Close();
netezzaConn = null;
}
TimeSpan duration = DateTime.Now.Subtract(Prequery);
Console.WriteLine("Query Time: " + duration);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统数据;
使用System.Data.OleDb;
使用System.Data.Sql;
使用System.Data.SqlTypes;
使用System.Data.SqlClient;
使用System.Security.Cryptography;
使用System.IO;
命名空间RemoveVoices
{
类UpdateNetezza
{
public bool NetezzaWorkFailure=false;
私有void NetezzaWorkFailed()
{
NetezzaWorkFailure=true;
}
公共无效更新计数(列出记录计数)
{
string connString=string.Format(“Provider=NZOLEDB;数据源={0};初始目录=EBIDW;用户ID=MYUSERNAME;密码={1}”,环境,密码);
OLEDB连接netezzaConn=null;
//由于超时问题,我正在快速输入timespan,以便在日志中记录每天批量更新所用的时间
DateTime Prequery=DateTime.Now;
int retrycount=0;
尝试
{
netezzaConn=新的OLEDB连接(connString);
netezzaConn.Open();
for(int i=0;i3)
{
WriteLine(“满足的最大重试次数。立即取消”);
抛出新的System.Exception();
}
其他的
{
i=i-1;
WriteLine(“查询超时,重试”);
}
}
}
}
捕获(例外情况除外)
{
控制台写入线(ex);
NetezzaWorkFailed();
}
最后
{
if(netezzaConn!=null)
{
netezzaConn.Close();
netezzaConn=null;
}
TimeSpan duration=DateTime.Now.Subtract(预查询);
Console.WriteLine(“查询时间:+持续时间”);
}
}
}
}
这可能更适合作为评论,但我没有足够的代表
我很少使用OleDB,但我们以与ODBC类似的方式运行查询,并且没有任何问题。至少尝试一下会很有趣
对您的代码片段发表一些评论,我怀疑这些评论对您的问题会有多大影响,但会有助于澄清和缩短代码
- 在连接上调用
而不是Dispose
Close
- 另外,处置您的命令对象
- 使用参数而不是格式化查询字符串(适用于和)。在循环外部创建OLEDB命令并添加三个参数。在循环中,您可以设置参数的
,并像以前一样执行查询值
- 使用
块而不是using
-try
,以避免显式调用catch
。如果要处理异常,仍然需要使用Dispose
块在
内部或外部使用
-try
块catch