超时和对象引用未设置对象 [英] Time out and object reference not set an object

查看:57
本文介绍了超时和对象引用未设置对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正面临超时和对象引用,当从作业运行C#exe时,不会设置对象的实例。应用程序运行查询并将结果保存为CSV。这个问题只发生了一段时间而且并非总是如此。



- 我注意到的其他几件事情:

Command.commandtimeout = 600

使用SQLreader存储结果查询。





我运行了探查器并发现任何死锁或长时间运行的查询,但我没有找到任何。试图运行每个查询以检查时间并且运行时间低于5秒,命令超时为10 S.



以下是我们得到的错误运行exe:



'

消息:超时已过期。完成
e操作之前经过的超时时间或服务器没有响应。
内部:
来源:.Net SqlClient数据提供者
Stack:at System.Data.SqlClient.SqlConnection.OnError(SqlException excepti
on,Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常
,布尔breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run (RunBehavior runBehavior,SqlCommand cm
dHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,Tds
ParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data .SqlClient.SqlDataReader.get_MetaData()
在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,Run
Behavior runBehavior,String resetOptionsString)
at System.Data.SqlClient.SqlCommand。 RunExecuteReaderTds(CommandBehavior cmdBe
havior,RunBehavior runBehavior,Boolean returnStream,Boolean async) System.Data.SqlClient.SqlCommand.RunExecuteReader上的
(CommandBehavior cmdBehav
ior,RunBehavior runBehavior,Boolean returnStream,String方法,DbAsyncResult
结果)
在System.Data.SqlClient。 SqlCommand.RunExecuteReader(CommandBehavior cmdBehav
ior,RunBehavior runBehavior,Boolean returnStream,String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior,S
tring method)
at System.Data.SqlClient.SqlCommand.ExecuteReader()
at ABC_Ad_Hoc_Emailer.Program.Run_Report()in F:\ ABC_code\Ad Hoc Emailer\ABC
Ad Hoc Emailer\ABC Ad Hoc Emailer\Program.cs:第209行
消息:对象引用未设置为对象的实例。
内部:
来源:ABC XYZ电子邮件
堆栈:位于F:\ ABCC_
code \Ad Hoc Emailer \ ABC中的ABC_Emailer.Program.Send_Mail(字符串内容) Ad Hoc Emailer\ABC Ad Hoc Emailer\Program.cs:第455行
at ABC_XYZEmailer.Program.Main(String [] Args)F:\ ABCC_code \ XXXY电子邮件
er \ ABC XYZ Emailer \ABC XYZ Emailer\Program.cs:第153行

'



以下是209行的代码(突出显示为粗体字母):



 SqlConnection db_Conn = new SqlConnection(connstring); 
db_Conn.Open();
< pre> SqlCommand command = new SqlCommand(Query,db_Conn);
command.CommandTimeout = 600;
< big> SqlDataReader Results = command.ExecuteReader();< / big>





任何人都可以帮我找出根本原因。


问候

Prashant

解决方案

要解决的第一件事是错误:对象引用未设置为对象的实例。



基于错误堆栈调查中的代码

 F:\ ABC_code \Ad Hoc Emailer \ABC Ad Hoc Emailer\ABC Ad Hoc Emailer\Program.cs:


第209行


你使用了一些对象,但实例未设置。在没有看到所有相关代码的情况下,无法确定未设置对象的原因是什么。


 SqlConnection db_Conn =  new  SqlConnection(connstring); 
// 尝试增加连接超时
db_Conn.ConnectTimeout = 600


Hi All,

I am facing the time out and object reference not to set an instance of an object when running an C# exe from the job . The application runs the querries and saves the results as CSV . This issue is happening only some times and not always.

-- Few other things I noticed:
Command.commandtimeout=600
Using SQLreader to store the resultant query.


I have ran the profiler and to find out any deadlock or long running querries but I didnt find any. Tried to run the each and every query to check the timings and are running below 5 secs and command time out is 10 S.

Following is the error which we got when running the exe :

'

Message : Timeout expired.  The timeout period elapsed prior to completion of th
e operation or the server is not responding.
Inner   :
Source  : .Net SqlClient Data Provider
Stack   :    at System.Data.SqlClient.SqlConnection.OnError(SqlException excepti
on, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception
, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cm
dHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, Tds
ParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, Run
Behavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBe
havior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehav
ior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult
result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehav
ior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, S
tring method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader()
   at ABC_Ad_Hoc_Emailer.Program.Run_Report() in F:\ABC_code\Ad Hoc Emailer\ABC
Ad Hoc Emailer\ABC Ad Hoc Emailer\Program.cs:line 209
Message : Object reference not set to an instance of an object.
Inner   :
Source  : ABC XYZ Emailer
Stack   :    at ABC_Emailer.Program.Send_Mail(String contents) in F:\ABC_
code\Ad Hoc Emailer\ABC Ad Hoc Emailer\ABC Ad Hoc Emailer\Program.cs:line 455
   at ABC_XYZEmailer.Program.Main(String[] Args) in F:\ABC_code\XYZ Email
er\ABC XYZ Emailer\ABC XYZ Emailer\Program.cs:line 153

'

Following is the code at 209 line(highlighted with bold letters):

SqlConnection db_Conn = new SqlConnection(connstring); 
				db_Conn.Open();
<pre>SqlCommand command = new SqlCommand(Query, db_Conn);	
				command.CommandTimeout = 600;
				<big>SqlDataReader Results = command.ExecuteReader();</big>



Can any one please help me in finding out the root cause.

Regards
Prashant

解决方案

The first thing to solve is the error: "Object reference not set to an instance of an object."

Based on the error stack investigate the code in

F:\ABC_code\Ad Hoc Emailer\ABC Ad Hoc Emailer\ABC Ad Hoc Emailer\Program.cs: 


on line 209
There you use some object but the instance is not set. Without seeing all relevant code it's impossible to say what the reason is for the object not being set.


SqlConnection db_Conn = new SqlConnection(connstring); 
//try increasing connection timeout
db_Conn.ConnectTimeout = 600;


这篇关于超时和对象引用未设置对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆