Reputation: 1885
In C#, I create some dataset or datatable from some query.
Data which comes from DateTime columns in the database are typed as System.String.
Is there a way to know programatically identify a column as containing a DateTime information ?
Sames question for boolean data which is considered as System.Int32 ?
Thank you
EDIT1 because it seems that this wasn't totally understood:
MyDataColumn.DataType is "System.String" (for DateTime) !
MyDataColumn.DataType is "System.Int32" (for Boolean) !
So I can't get the in I want from DataType, otherwise I wouldn't even have posted this.
EDIT2 And the TryParse() idea has a big problem here, it's more obvious with the Boolean/System.Int32 confusion: If I see "1" or "0" I could think that it's a Boolean while it may as well be an Int32 column. TryParse() has the same flaw.
Upvotes: 3
Views: 6543
Reputation: 32481
EDIT:
To check the containing data you may try to parse its values. so for each value in that column you do like this:
DateTime dateTime;
DateTime.TryParse("your string", out dateTime);
Upvotes: 3
Reputation: 223247
You can use LINQ to see if any column of DateTime
type exists in your datatable. You need to parse each column with DateTime.TryParse
to see if any column gets successfully parsed like:
if (dt == null || dt.Rows.Count <= 0)
return false;
DateTime temp;
if (dt.Rows[0].ItemArray.Any(r => DateTime.TryParse(r.ToString(),
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out temp)))
{
//Column exists.
}
If you want to get the column index then you can try:
var column = dt.Rows[0].ItemArray
.Select((r, index) => new { Value = r, Index = index })
.FirstOrDefault(t => DateTime.TryParse(t.ToString(),
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out temp));
if(column != null)
Console.WriteLine(column.Index);
(Remember to include System.LINQ)
Upvotes: 0
Reputation: 6918
Create a stored proc like the follow:
Alter proc proc_GetDataTypeValueByColumnName
(
@TableName varchar(500)='tblAdminUser',
@ColumnName varchar(500)='Id'
)
as
declare @DataType varchar(500);
if exists(select 1 from sys.tables where name=@TableName)
begin
set @DataType=(SELECT t.Name 'Data type'FROM sys.columns c INNER JOIN
sys.types t ON c.system_type_id = t.system_type_id
WHERE
c.object_id = OBJECT_ID(''+@TableName+'') and c.name=@ColumnName)
-- select dbo.GetDataTypeValue(@DataType)
select @DataType
End
You need to pass the table name and the column name of which you want to get the datatype if you want to get it in the code then you can go for the following stuff:
public String GetNumberForDataTypeofColumn(String TableName, String ColumnName)
{
SqlParameter[] param = new SqlParameter[2];
param[0] = new SqlParameter("@TableName", TableName);
param[1] = new SqlParameter("@ColumnName", ColumnName);
String result = SqlHelper.ExecuteScalar(ConfigurationManager.ConnectionStrings["cn"].ConnectionString, CommandType.StoredProcedure, "proc_GetDataTypeValueByColumnName", param).ToString();
return result;
}
And there is another way that is :: data Column has property "DataType" you can also check from that like below:
dtItems.Columns[0].DataType
Hope this will help you
Upvotes: 1
Reputation: 906
if (myDataTable.Columns["thisColumn"].DataType == System.Type.GetType("System.DateTime"))
{
...
}
else
{
}
Upvotes: 4