SqlCommand读取一个值

use*_*erS 2 c# sql sql-server sqlcommand

我有一个问题,返回的值SqlCommand,我有这个代码:

string sqlSelect = "Select TOP 1 Quotation.SentToSupp as SentToSupp FROM Quotation JOIN Notifications ON Quotation.QuotationId = QuotationID ";

SqlCommand Comm = new SqlCommand(sqlSelect, this.Connection);
Run Code Online (Sandbox Code Playgroud)

sqlSelect查询选择第一个DateTime值.当我打电话给SqlCommand我想要获得这个值(只有一个值).我添加查询和我的连接很好.

但我不知道如何获得我的DateTime价值......必须使用类似的东西ExecuteReader

先感谢您!!

Ste*_*eve 8

ExecuteReader工作但需要更多对象和更多代码 - (An SqlDataReader,调用Read和Extract值).相反,你可以简单地使用对象的ExecuteScalar方法SqlCommand(它只返回结果集的第一行的第一列)

string sqlSelect = "Select TOP 1 Quotation.SentToSupp as SentToSupp FROM ....";
SqlCommand Comm = new SqlCommand(sqlSelect, this.Connection);
object result = Comm.ExecuteScalar();
if(result != null)
   DateTime dtResult = Convert.ToDateTime(result);
Run Code Online (Sandbox Code Playgroud)

如果由于某种原因,返回的结果中没有记录,请注意ExecuteScalar可能返回null值的事实