[英]How to read the date that is return from a stored procedure which have no parameter using asp.net?
ALTER PROCEDURE [dbo].[usp_currentDate]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT CONVERT(VARCHAR(10), GETDATE(), 101)
END
This is my stored procedure code. 这是我的存储过程代码。 I got the correct date, but I am unable to read the values in asp.net.
我得到了正确的日期,但是我无法读取asp.net中的值。
public DateTime getCurrentDate()
{
try
{
DateTime dt;
SqlDataReader reader;
SqlConnection objSqlConn = new SqlConnection(ConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_currentDate";
cmd.Connection = objSqlConn;
objSqlConn.Open();
me(1);
reader = cmd.ExecuteReader();
dt = reader.GetDateTime(1);
return dt;
}
catch (Exception ex)
{
throw ex;
}
}
but it's not working. 但它不起作用。 It throws an exception
引发异常
Invalid attempt to read when no data is present
没有数据时读取尝试无效
What changes do I have to make? 我必须进行哪些更改?
Try something like this, using ExecuteScalar
to fetch a single row / single column value: 尝试使用
ExecuteScalar
这样的方法来获取单行/单列值:
public DateTime getCurrentDate()
{
try
{
DateTime dt;
// set up connection and command in *using* blocks
using (SqlConnection objSqlConn = new SqlConnection(ConnString))
using (SqlCommand cmd = new SqlCommand("dbo.usp_currentDate", objSqlConn))
{
cmd.CommandType = CommandType.StoredProcedure;
objSqlConn.Open();
// since the query returns exactly one row, one column -> use ExecuteScalar
object result = cmd.ExecuteScalar();
// if something was returned .....
if (result != null)
{
// try to convert to DateTime
if (DateTime.TryParse(result.ToString(), out dt)
{
return dt;
}
}
// return default value, if no data was read or could not be converted
return DateTime.MinValue;
}
}
catch (Exception ex)
{
throw;
}
}
Besides on why you return local time with GETDATE
as a character, you can't read the data reader without move the cursor to it's first line with Read
method . 除了说明为什么要使用
GETDATE
作为字符返回本地时间之外,如果不使用Read
方法将光标移到数据的第一行,就无法读取数据读取器。
From documentation; 从文件;
The default position of the
SqlDataReader
is before the first record.SqlDataReader
的默认位置在第一条记录之前。 Therefore, you must callRead
to begin accessing any data.因此,您必须调用
Read
才能开始访问任何数据。
string s;
reader = cmd.ExecuteReader();
if(reader.Read())
s = reader.GetString(0);
Or better, use ExecuteScalar
method since you return one row with one column like; 更好的方法是使用
ExecuteScalar
方法,因为您返回的是一行和一列,例如:
string s = (string)cmd.ExecuteScalar();
Remember, CONVERT(VARCHAR(10), GETDATE(), 101)
returns varchar
which is preferable used with string
in CLR side, that's why you can't assign this value to a DateTime
variable. 记住,
CONVERT(VARCHAR(10), GETDATE(), 101)
返回varchar
,它最好在CLR端与string
一起使用 ,这就是为什么不能将此值分配给DateTime
变量的原因。
Also use using
statement to dispose your database connections and commands. 还可以使用
using
语句来处理数据库连接和命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.