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