我有一个存储过程,它在手动执行时工作正常,但是当在代码上调用它时,由于超时,它返回空引用异常。这是代码:
private DataTable GetProcedure(DateTime dtStart, DateTime dtEnd) {
DataTable dt = new DataTable();
SqlDataReader reader;
string cs = //Connection String, it works everywhere, no need to put the code.
SqlConnection sqlConnection = new SqlConnection(cs);
string cmdText = "ProcedureName";
SqlCommand cmd = new SqlCommand(cmdText, sqlConnection);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter pFechaInicio = new SqlParameter("@dtStart", dtStart);
SqlParameter pFechaFin = new SqlParameter("@dtEnd", dtEnd);
cmd.Parameters.Add(pFechaInicio);
cmd.Parameters.Add(pFechaFin);
sqlConnection.Open();
reader = cmd.ExecuteReader(); //NULL REFERENCE EXCEPTION HERE
dt.Load(reader);
sqlConnection.Close();
return dt;
}
我使用完全相同的参数手动调用了存储过程,它运行良好(存储过程返回 3 行/2 列结果,没有任何 NULL。
更重要的是,这个错误在 1 个月前开始发生,但该程序已经完美运行了 6 个月。
编辑:这是关于 SP 的一些信息:
SP头:
ALTER PROCEDURE [dbo].[SPName]
@dtStart datetime, @dtEnd datetime
AS
BEGIN
SET NOCOUNT ON;
select column1 as Name1, column2 as Name2
/* doesn't matter */
END
我手动执行 SP 时得到的具体结果是:
column1 column2
1.0000000 105.426890
2.0000000 96.316330
3.0000000 82.849690
这是整个错误代码:
en System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
en System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
en System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
en System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
en System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
en System.Data.SqlClient.SqlDataReader.get_MetaData()
en System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
en System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
en System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
en System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
en System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
en System.Data.SqlClient.SqlCommand.ExecuteReader()
en BACK.OFERTAS.COBERTURAS.PRECIOS._01._00.StartJob.GetProcedure(DateTime dtStart, DateTime dtEnd) en C:\Users\Dell\Documents\BACK.OFERTAS.COBERTURAS.PRECIOS.01.00\StartJob.cs:línea 278
en BACK.OFERTAS.COBERTURAS.PRECIOS._01._00.StartJob.Start() en C:\Users\Dell\Documents\BACK.OFERTAS.COBERTURAS.PRECIOS.01.00\StartJob.cs:línea 176
重新阅读整个 stackTrace 似乎是超时?怎么可能呢,SP 不到 1 秒就被执行了。