Sha Le Sha Le - 1 year ago 124
SQL Question

c# Column datatype Date type (NOT DateTime)

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

This what currently I do:

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

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

formatedVal = val.ToString();

Answer Source

Nope. There is no Date type, there is only DateTime. If this is coming from a SQL Server 2008 table that uses a date column, it will be mapped to DateTime in .NET.

If you need to get at this information then you'll have to do it at the SQL level; by the time it's loaded into a DataTable, it's too late.

I'm not positive about your requirements, but it looks like you might be able to get away with just checking for midnight and using a different format for that:

DateTime dt = (DateTime)val;
return (dt == dt.Date) ? Formatter.GetDate(dt) : Formatter.GetDateTime(dt);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download