本文介绍了DbCommand.ExecuteScalar()的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好,所有 而 SqlDataReader.GetValue()需要返回本机.NET类型的值,没有 DbCommand.ExecuteScalar() 。但是,建议使用GetcuteTalue()的ExecuteScalar()instad从数据库中检索单个值,原因是它需要的代码较少。这是否假设关于返回类型,两个方法表现理想,因此ExecuteScalar()也需要始终返回本机.NET类型,并且它是标准ifit返回其他任何内容的错误和翻译? 解决方案 ExecuteScalar仅返回数据集第一行的第一个值。内部处理就像ExecuteReader()一样,打开DataReader,选择值,然后DataReader被销毁。 请检查 .net framework 代码。 覆盖公共对象ExecuteScalar(){ SqlConnection.ExecutePermission.Demand(); //在进入任何Execute时重置_pendingCancel - 用于在进入Execute * API和获取stateObject的线程之间同步状态 //。 _pendingCancel = false; SqlStatistics statistics = null; IntPtr hscp; Bid.ScopeEnter(输出hscp,"< sc.SqlCommand.ExecuteScalar | API>%d#",ObjectID); Bid.CorrelationTrace("< sc.SqlCommand.ExecuteScalar | API | Correlation> ObjectID%d#,ActivityID%ls \ n",ObjectID); bool success = false; int? sqlExceptionNumber = null; try { statistics = SqlStatistics.StartTimer(Statistics); WriteBeginExecuteEvent(); SqlDataReader ds; ds = RunExecuteReader(0,RunBehavior.ReturnImmediately,true,ADP.ExecuteScalar); object result = CompleteExecuteScalar(ds,false); success = true; 返回结果; } catch(SqlException ex){ sqlExceptionNumber = ex.Number; throw; } finally { SqlStatistics.StopTimer(statistics); Bid.ScopeLeave(ref hscp); WriteEndExecuteEvent(success,sqlExceptionNumber,synchronous:true); } } 究竟在ExecuteReader中发生了什么。另一个优点是ExecuteScalar在没有读取数据时返回null。如果你使用ExecuteReader,你必须自己检查一下。 祝你好运, 章龙 Hello, allWhereas SqlDataReader.GetValue() is required to return a value of a native .NET type, there is no such requirement ofDbCommand.ExecuteScalar(). There is, however, a recommendation to use ExecuteScalar() instad of GetValue() to retrieve a single value from the database for the sole reason that it takes less code. Does that assume that with respect to return type both methnods behave identially and therefore ExecuteScalar() is also required always to return a native .NET type and that it is an error and vilation of the standard ifit returns anything else? 解决方案 Hi,ExecuteScalar only returns the first value from the first row of the dataset. Internal it is treated just like ExecuteReader(), a DataReader is opened, the value is picked and the DataReader gets destroyed afterwards. Please check the .net framework code. override public object ExecuteScalar() { SqlConnection.ExecutePermission.Demand(); // Reset _pendingCancel upon entry into any Execute - used to synchronize state // between entry into Execute* API and the thread obtaining the stateObject. _pendingCancel = false; SqlStatistics statistics = null; IntPtr hscp; Bid.ScopeEnter(out hscp, "<sc.SqlCommand.ExecuteScalar|API> %d#", ObjectID); Bid.CorrelationTrace("<sc.SqlCommand.ExecuteScalar|API|Correlation> ObjectID%d#, ActivityID %ls\n", ObjectID); bool success = false; int? sqlExceptionNumber = null; try { statistics = SqlStatistics.StartTimer(Statistics); WriteBeginExecuteEvent(); SqlDataReader ds; ds = RunExecuteReader(0, RunBehavior.ReturnImmediately, true, ADP.ExecuteScalar); object result = CompleteExecuteScalar(ds, false); success = true; return result; } catch (SqlException ex) { sqlExceptionNumber = ex.Number; throw; } finally { SqlStatistics.StopTimer(statistics); Bid.ScopeLeave(ref hscp); WriteEndExecuteEvent(success, sqlExceptionNumber, synchronous: true); } }Exactly what happens inside ExecuteReader. Another advantage is that ExecuteScalar returns null when no data is read. If you use ExecuteReader, you'd have to check this yourself.Best regards,Zhanglong 这篇关于DbCommand.ExecuteScalar()的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 05:16