I have the following code :
public void InsertDataInBulk(DataTable dataTable, string functionName , ILogger logger)
{
try
{
var connection = _dbConnect.OpenConnection(logger);
DbCommand command = connection.CreateCommand();
command.CommandText = functionName;
command.CommandType = CommandType.StoredProcedure;
if (dataTable != null)
{
foreach (DataColumn column in dataTable.Columns)
{
var parameter = command.CreateParameter();
parameter.ParameterName = column.ColumnName;
Type type = column.DataType;
parameter.Value = dataTable.AsEnumerable().Select(r => r.Field<string>(column.ColumnName)).ToArray();
command.Parameters.Add(parameter);
}
}
_dbConnect.CloseConnection(connection, logger);
}
catch (Exception ex)
{
logger.LogError(ex, ex.Message);
throw;
}
}
I have one issue in this code and that is : I want to make the following piece of code as generic code:
Type type = column.DataType;
parameter.Value = dataTable.AsEnumerable().Select(r => r.Field<string>(column.ColumnName)).ToArray();
I just want to pass the DataType that I have found in the variable type
in the next line as
r => r.Field<type>
just to make my code generic as the datatable that I am getting in this function consists of columns of many different data types.
But it gives me compiler error that type is a variable but used like a Type
.
How can I solve this ?
Please help.
Thank you