简体   繁体   English

如何使用asp.net读取没有参数的存储过程返回的日期?

[英]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 call Read 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.

相关问题 如何使用存储过程从 ASP.NET 记录计数? - How to record count from ASP.NET using stored procedure? I have custom data which is return from stored procedure so I need to add those data into list&lt;&gt; and return in asp.net core web api c# - I have custom data which is return from stored procedure so I need to add those data into list<> and return in asp.net core web api c# 从ASP.NET WinForm向存储过程传递参数 - Passing parameter to stored procedure from ASP.NET WinForm 如何在ASP.NET中使用输出参数插入存储过程 - How to use the output parameter insert stored procedure in ASP.NET 从asp.net中的存储过程返回一行 - Return one row from stored procedure in asp.net 从asp.net中的存储过程中获取返回值 - Get Return Value from Stored procedure in asp.net 如何从ASP.NET中的Mysql存储过程调用输出参数 - How to call an Output Parameter from a Mysql stored procedure in ASP.NET 如何将表 OBJECT 输出参数从 C# ASP.NET 核心传递到 Z30162ED78B6C10F231411F2F4 - How to pass a TABLE OBJECT out parameter from C# ASP.NET Core to Oracle Stored Procedure 如何从oracle存储过程值中读取数据到asp.net中的dropdownlist中 - How to read data from oracle stored procedure value into dropdownlist in asp.net 如何使用存储过程结果在 ASP.NET MVC 中将列表项作为 JSON 返回? - How to return list item as JSON in ASP.NET MVC using stored procedure result?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM