mercredi 17 août 2016

FastMember and nullable properties

i'm having an issue using fastmember, where by i have a public nullable property (datetime) for my class, but when i try and insert it into the db the column value is an empty string an not a null, which causes an exception to be thrown, any ideas on how to make the column value null?

public class MyClass
{
    public DateTime? MyDate {get;set;}
}

DataTable testTable = new DataTable();
using (var reader = ObjectReader.Create(myClassList))
{
    testTable.Load(reader);
}

 using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString))
 {
     SqlTransaction transaction = null;
     connection.Open();
     try
     {
         transaction = connection.BeginTransaction();
         using (var sqlBulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.TableLock, transaction))
         {
              sqlBulkCopy.BulkCopyTimeout = 0;
              sqlBulkCopy.BatchSize = batchSize;
              sqlBulkCopy.DestinationTableName = "myTable";
              for (var cols = 0; cols < testTable.Columns.Count; cols++)
              {
                   sqlBulkCopy.ColumnMappings.Add(cols, cols);
              }

              sqlBulkCopy.WriteToServer(testTable);
          }
          transaction.Commit();
      }
      catch (Exception ex)
      {
          transaction.Rollback();
          throw ex;
      }
  }

if releasedate is null then it will always put it into the table as a blank string rather than a null, is there a way to force it to be null? or to interpret the empty string as a null in the sql bulk copy?





Aucun commentaire:

Enregistrer un commentaire