从DataRow(C#)检索DateTime值

时间:2021-04-22 10:20:11

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

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

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)
                        {

8 个解决方案

#1


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.Value and 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 
}

#2


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 TryParse depending on your needs.

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

#3


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;

#4


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以确保按预期解析格式。

#5


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()并将其打印到控制台(或暂停并在调试器中查看)。如果它说它是一个字符串,那么上述大多数建议都会有所帮助。如果它说了别的话,请回来更新你的问题。

#6


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

作为旁边的答案,您还可以使用静态函数Convert.ToDateTime

#7


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())是要走的路,但它会抛出“字符串未被识别为有效的DateTime”错误。你能在r [“FISS”]栏中显示实际的字符串,这可能是一个国际化问题....

#8


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 document for 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)

#1


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.Value and 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 
}

#2


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 TryParse depending on your needs.

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

#3


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;

#4


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以确保按预期解析格式。

#5


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()并将其打印到控制台(或暂停并在调试器中查看)。如果它说它是一个字符串,那么上述大多数建议都会有所帮助。如果它说了别的话,请回来更新你的问题。

#6


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

作为旁边的答案,您还可以使用静态函数Convert.ToDateTime

#7


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())是要走的路,但它会抛出“字符串未被识别为有效的DateTime”错误。你能在r [“FISS”]栏中显示实际的字符串,这可能是一个国际化问题....

#8


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 document for 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)