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

时间:2022-04-01 21:55:36
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?

我需要做些什么改变?

2 个解决方案

#1


1  

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;
    }
} 

#2


0  

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. Therefore, you must call Read to begin accessing any data.

SqlDataReader的默认位置在第一个记录之前。因此,您必须调用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端的字符串一起使用,这就是为什么你不能将这个值赋给DateTime变量的原因。

Also use using statement to dispose your database connections and commands.

还可以使用using语句来处理数据库连接和命令。

#1


1  

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;
    }
} 

#2


0  

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. Therefore, you must call Read to begin accessing any data.

SqlDataReader的默认位置在第一个记录之前。因此,您必须调用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端的字符串一起使用,这就是为什么你不能将这个值赋给DateTime变量的原因。

Also use using statement to dispose your database connections and commands.

还可以使用using语句来处理数据库连接和命令。