简体   繁体   English

无法将类型“System.Data.DataSet”隐式转换为“System.DateTime”

[英]Cannot implicitly convert type 'System.Data.DataSet' to 'System.DateTime'

I am trying to get system date through this method but is showing error saying :我正在尝试通过此方法获取系统日期,但显示错误提示:

Cannot implicitly convert type 'System.Data.DataSet' to 'System.DateTime'

Below is the method I am using.下面是我正在使用的方法。 Any suggestions are appreciated.任何建议表示赞赏。 As why is this happening and how can I resolve it?至于为什么会发生这种情况,我该如何解决?

public DateTime GetLocalTime()
        {
            OracleBridge ob = new OracleBridge(_connStr);
            string sqlQuery = "select sysdate from dual";

            DateTime dt = new DateTime();
            try
            {

                dt = Convert.ToDateTime(ob.ExecuteScalar(sqlQuery, CommandType.Text));    //this line is giving error

            }
            catch (OracleException ex)
            {
                throw ex;
            }

            return dt;
        }

You cannot convert, as the exception says, a DataSet type to a DateTime .正如异常所说,您不能将DataSet类型转换为DateTime

dt = ob.ExecuteQuery(sqlQuery, CommandType.Text);

That line is wrong.那条线错了。 If you are expecting a only row result with a DateTime value, then you have to use the ExecuteScalar method and cast the result as DateTime:如果您期望只有一个带有 DateTime 值的行结果,那么您必须使用ExecuteScalar方法并将结果转换为 DateTime:

dt = Convert.ToDateTime(yourOracleConnection.ExecuteScalar(sqlQuery, CommandType.Text)); //I assume you are using ADO.NET Library

If you are not expecting that result, you have to access to the specific row you are looking for, and then get the value as this:如果您不期望该结果,则必须访问您要查找的特定行,然后获取如下值:

dt = Convert.ToDateTime(ob.ExecuteQuery(sqlQuery, CommandType.Text).Tables[0].Rows[0]["YourColumnName"]); //You´ll have to check out where is the value you want and replace on the Rows[0]

If it is just one value that you are trying to retrieve then use ExecuteScalar method which will return an object.Convert it to datetime如果它只是您尝试检索的一个值,则使用ExecuteScalar方法将返回一个对象。将其转换为日期时间

    public DateTime GetLocalTime()
    {
        OracleBridge ob = new OracleBridge(_connStr);
        string sqlQuery = "select distinct sysdate from dual";

        DateTime dt = new DateTime();
        try
        {
            dt = Convert.ToDateTime(ob.ExecuteScalar(sqlQuery, CommandType.Text));
        }
        catch (OracleException ex)
        {
            throw ex;
        }

        return dt;
    }

Answers provided already should be sufficant, in terms of syntax, if you throw the exception further all you actually need is就语法而言,已经提供的答案应该足够了,如果您进一步抛出异常,您实际需要的是

catch (OracleException)
{
   throw;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型'int'隐式转换为'system.data.dataset' - cannot implicitly convert type 'int' to 'system.data.dataset' 不能隐式转换类型System.DateTime? 到System.DateTime - cannot implicitly convert type System.DateTime? to System.DateTime 错误5:无法将类型'System.Data.DataSet'隐式转换为'System.Data.SqlClient.SqlDataAdapter' - Error 5: Cannot implicitly convert type 'System.Data.DataSet' to 'System.Data.SqlClient.SqlDataAdapter' 无法将类型“System.Data.SqlClient.SqlDataReader”隐式转换为“System.Data.DataSet” - Cannot implicitly convert type 'System.Data.SqlClient.SqlDataReader' to 'System.Data.DataSet' 无法将类型'int'隐式转换为'System.Data.Dataset'-需要SQL返回的结果 - Cannot implicitly convert type 'int' to 'System.Data.Dataset' - returned result from SQL required 无法在服务器端将类型'System.Data.DataSet'隐式转换为'string'访问Div - Cannot implicitly convert type 'System.Data.DataSet' to 'string' access Div in Server Side 无法隐式转换类型'System.DateTime?'到'System.DateTime'。存在显式转换 - Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists 无法隐式转换类型'System.DateTime?' 到LINQ查询中的“ System.DateTime” - Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime' in LINQ query C#无法将类型'string'隐式转换为'System.DateTime' - C# Cannot implicitly convert type 'string' to 'System.DateTime' 错误返回时无法将类型'string'隐式转换为'System.DateTime' - Error Cannot implicitly convert type 'string' to 'System.DateTime' on return
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM