C # data type column Date Type (NOT DateTime)

I want to know if there is a good way to define a Column DataType for a Date field (NOT DateTime)?

This is what I am doing now:

switch (dt.Columns[col].DataType.FullName)
{  
    case "System.DateTime":  
        formatedVal = Formatter.GetDateTime(val);  
        break;

    // which is NOT possible, but something equivalent am looking for
    case "System.Date":  
        formatedVal = Formatter.GetDate(val);  
        break;

    default:
        formatedVal = val.ToString();
        break;
}
+3
source share
5 answers

Nope. No type Date, only exists DateTime. If this comes from a SQL Server 2008 table that uses a column Date, it will appear in DateTime. NET.

If you need to get this information, you will need to do it at the SQL level; by the time it boots in DataTable, it's too late.

, , :

DateTime dt = (DateTime)val;
return (dt == dt.Date) ? Formatter.GetDate(dt) : Formatter.GetDateTime(dt);
+7

.

switch (dt.Columns[col].DataType.FullName)
{
    case "System.DateTime":
        formatedVal = ((DateTime)dataRow[col]).ToString("d");
        break;

    case "System.Date":
        formatedVal = ((DateTime)dataRow[col]).ToString();
        break;

    default:
        formatedVal = dataRow[col].ToString();
        break;
}
+1

SqlDataReader, GetSchemaTable, SQL Server - , , MSDN. , SQL Server.

SqlDataAdapter FillSchema, , db.

0

, "" - CLR, "". IDE ...

In any case, since in any case, when you want to use the type of an object, I think obj.GetType (). The name is not the best way, because you end up comparing strings when you really want to find out obj is a keratin type.

I would go with

if (dt.Columns[col] is DateTime)
.....
else
  if (dt.Columns[col] is Date) 
   ... 

I am just showing another way to perform data type checks. You told yourself that it is impossible to compare with the "Date".

0
source

Source: https://habr.com/ru/post/1741288/


All Articles