从 DataRow 中检索 DateTime 值 (C#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1106204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:23:10  来源:igfitidea点击:

Retrieving a DateTime value from a DataRow (C#)

c#datetimeado.net

提问by Developer

How can I get DateTimevalue in C# from row, the current code is giving me error any help is appreciated, the data is coming in from progress database:

如何DateTime从行中获取C# 中的值,当前代码给我错误任何帮助表示赞赏,数据来自进度数据库:

foreach (DataRow r in ds.Tables[0].Rows)
{
    string prodCode = r["PRD-CDE"].ToString();
    statCode = r["STAT"].ToString();
    DateTime firstIssueDate = (DateTime)(r["FISS"]); 
    DateTime endIssueDate = (DateTime)(r["EISS"]);
    if(endIssueDate > DateTime.Now)
    { /*do some thing...*/}
    else {/*user invalid...*/}
}

there are two ways used in getting date convert and pars, thank you all for the help

有两种方法用于获取日期转换和解析,谢谢大家的帮助

Final working Code snippet:

最终工作代码片段:

foreach (DataRow r in ds.Tables[0].Rows)
            {
                string prodCode = r["PRD-CDE"].ToString(),statCode = r["STAT"].ToString();
               // r.<DateTime?>("FISS");
                if (r["FISS"] != DBNull.Value)
                {
                    DateTime firstIssueDate = Convert.ToDateTime(r["FISS"]);
                    if (r["EISS"] != DBNull.Value)
                    {
                        DateTime endIssueDate = DateTime.Parse(r["EISS"].ToString());
                        if (endIssueDate > DateTime.Now)
                        {

采纳答案by Robert Luong

This is just a guess but if the corresponding type in the database is DateTime, could you check if the column is nullable?

这只是一个猜测,但如果数据库中对应的类型是 DateTime,你能检查一下列是否可以为空吗?

If so you may want to do a check r["column"] == DBNull.Valueand then pass it to a nullable DateTime? Field.

如果是这样,您可能想要进行检查r["column"] == DBNull.Value,然后将其传递给可为空的 DateTime?场地。

Or even easier:

或者更简单:

row.Field<DateTime?>("column")

If it isn't then yeah, Convert.ToDateTime() or something else should do it.

如果不是,那么是的, Convert.ToDateTime() 或其他东西应该这样做。

EDIT:

编辑:

I see your final code there but is there any chance you want to do this:

我在那里看到了您的最终代码,但您是否有机会这样做:

DateTime? firstIssueDate = r.Field<DateTime?>("fiss"); 
DateTime? endIssueDate = r.Field<DateTime?>("eiss"); 

if (firstIssueDate.HasValue && endIssueDate.HasValue) 
{ 
    firstIssueDate.Value // blah blah 
    endIssueDate.Value // blah blah 
}

回答by Chris Thompson

I would recommend using DateTime.Parse() if the row is returning a string for that index.

如果该行返回该索引的字符串,我会建议使用 DateTime.Parse()。

 string prodCode = r["PRD-CDE"].ToString(),statCode = r["STAT"].ToString();
 DateTime firstIssueDate = DateTime.Parse(r["FISS"].ToString());
 DateTime endIssueDate = DateTime.Parse(r["EISS"].ToString());

You could also use TryParsedepending on your needs.

您也可以TryParse根据需要使用。

回答by Jhonny D. Cano -Leftware-

As a side answer, you could use also the static function Convert.ToDateTime

作为附带答案,您还可以使用静态函数 Convert.ToDateTime

回答by Colin

DateTime.Parse(r["FISS"].ToString()) is the way to go, but it throws a "String was not recognized as a valid DateTime" error. Could you show the actual string in the r["FISS"] column, it might be a internationalisation problem....

DateTime.Parse(r["FISS"].ToString()) 是要走的路,但它会抛出“字符串未被识别为有效的日期时间”错误。您能否在 r["FISS"] 列中显示实际字符串,这可能是一个国际化问题....

回答by Timothy Carter

foreach (DataRow r in ds.Tables[0].Rows)
{
    string prodCode = r["PRD-CDE"].ToString();
    string statCode = r["STAT"].ToString();
    DateTime firstIssueDate = DateTime.Parse((r["FISS"]).ToString()); 
    DateTime endIssueDate = DateTime.Parse((r["EISS"]).ToString());
    if(endIssueDate > DateTime.Now)
    { /*do some thing...*/}
    else {/*user invalid...*/}
}

This should compile and may work for you. Though it is certainly not performing any error checking that you should do for production code. Also look into DateTime.TryParse and you may to look into adding a IFormatProvider to ensure the format is parsed as expected.

这应该可以编译并且可能对您有用。尽管它肯定不会执行您应该对生产代码执行的任何错误检查。还要查看 DateTime.TryParse ,您可能会考虑添加 IFormatProvider 以确保按预期解析格式。

回答by Chansik Im

If you have a DateTime string with a special format (not any standard .NET DateTime format) that needs to be converted to .NET DateTime type, you can use DateTime.ParseExact()method.

如果您有需要转换为 .NET DateTime 类型的特殊格式(不是任何标准的 .NET DateTime 格式)的 DateTime 字符串,您可以使用DateTime.ParseExact()方法。

Please see the MSDN documentfor more details including examples.

请参阅MSDN 文档以获取更多详细信息,包括示例。

If you have multiple formats to parse, try DateTime.ParseExact Method (String, String[], IFormatProvider, DateTimeStyles)

如果您有多种格式要解析,请尝试DateTime.ParseExact Method (String, String[], IFormatProvider, DateTimeStyles)

回答by zvolkov

First of all, do r["FISS"].GetType() and print it to console (or pause and look at it in the debugger). If it says it's a String, then most of the above advices will help. If it says something else, please come back and update your question.

首先,执行 r["FISS"].GetType() 并将其打印到控制台(或暂停并在调试器中查看)。如果它说它是一个字符串,那么上面的大部分建议都会有所帮助。如果它说别的,请回来更新你的问题。

回答by brianary

If you want to use a default value (such as DateTime.MinValue), rather than null (DateTime?) or DBNull, you could do this:

如果您想使用默认值(例如 DateTime.MinValue),而不是 null (DateTime?) 或 DBNull,您可以这样做:

var firstIssueDate = r["FISS"] as DateTime? ?? DateTime.MinValue;
var endIssueDate = r["EISS"] as DateTime? ?? DateTime.MinValue;