SqlBulkCopy

  • I'm using the SQLBulkCopy to upload excel spreadsheets from my asp.net app, I want to add a date that the file was uploaded when the user uploads the file. Is there a way to add a dynamic column in the upload?

    I tried this but it failed:

    using (OleDbDataReader dr = command.ExecuteReader())

    {

    using (SqlBulkCopy t = new SqlBulkCopy(db()))

    {

    t.ColumnMappings.Add("name", 1);

    t.ColumnMappings.Add(UploadDate, System.Date.Now());

    }

    }

    and the file never uploads, the column name exists in the db and the 1 is the first column in my spreadsheet, the UploadDate is the date column in the table and I want to grab the date is was uploaded.

    Any Suggestions on how I can get this to work?

  • Is it possible just to default the column on your table to getdate()?

    In SQL Server Management Studio, right click on your table->Design->Click on Column Name->Put getdate() in "Defautl Value or Binding"

  • I tried that and it errors and the data is never uploaded to the table

  • Were you able to upload the data before adding this column into your mix? If not, then there is something with your ASP.net code. If yes, then after you add the default value to the database level, you don't need to add any code in ASP.net to populate this column.

  • yes, I can upload the file just fine, i want to add the upload date to the table which is failing

  • Ok. This seems more of a .Net issue, SqlBulkCopy is a .Net class.

    Looking at this link

    It seems ColumnMapping only takes real column names, so your syntax of trying to set a value to it is likely invalid.

    Are you sure if you set the default value on the Database value and is able to do a successful insert (without trying to set this or the NAME column) and there is still no value populated for this column? That would be very odd...

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply