Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Dataset table column data type in C#

Tags:

c#

I have a dataset contains 1 table where i want to iterate loop over the table.

Inside the loop i want to check if Column 1 is date type escape that column and move to next column.Please help me.

excelConnection = new OleDbConnection(connectionString);
                        //Get the name of First Sheet
excelConnection.Open();


excelSheet = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

string sheetName = excelSheet.Rows[0]["TABLE_NAME"].ToString();
excelConnection.Close();

string query = "SELECT * FROM [" + sheetName + "]";

excelCommand = new OleDbCommand(query, excelConnection);
excelDataAdapter = new OleDbDataAdapter(excelCommand);

excelDataAdapter.Fill(excelData);

Thanks in advance.

like image 245
Neeraj Dubey Avatar asked Dec 01 '25 19:12

Neeraj Dubey


2 Answers

   foreach (DataColumn col in dt.Columns)
     {
          if (col.DataType == typeof(DateTime)) continue;
          //Execute business logic for other columns
     }
like image 170
mlg Avatar answered Dec 03 '25 09:12

mlg


Try this code to get Datatype of Dataset table column:

if (Dataset1.Tables[0].Columns[1].ColumnName.ToLower().Contains("date") || Dataset1.Tables[0].Columns[1].DataType.ToString() == "System.DateTime")                              
{
   //Do work;
}

Hope you like it.

like image 23
Priti Chaturvedi Avatar answered Dec 03 '25 09:12

Priti Chaturvedi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!