I am copying excel file to mssql database's table, problem is this :

I have some informations in 4 rows but mssql database table has 13 rows. fisrt 4 rows has my information and 4-13 rows has null values. sbccm from 1 to 14. where is the problem I donw know but I want to save my information to DB without NULL values.

SqlBulkCopyColumnMapping sbccm1 = new SqlBulkCopyColumnMapping("Bildirim", "Bildirim");
                sbc.ColumnMappings.Add(sbccm1);
                SqlBulkCopyColumnMapping sbccm2 = new SqlBulkCopyColumnMapping("Bildirim Türü Adı", "CagriTipi");
                sbc.ColumnMappings.Add(sbccm2);
                SqlBulkCopyColumnMapping sbccm3 = new SqlBulkCopyColumnMapping("Malzeme", "Model");
                sbc.ColumnMappings.Add(sbccm3);

Recommended Answers

All 2 Replies

I don't see where you are actually copying the rows, but to limit them to only the first four, you set the rowState to indicate it should be copied:

//
        // Summary:
        //     Copies only rows that match the supplied row state in the supplied System.Data.DataTable
        //     to a destination table specified by the System.Data.SqlClient.SqlBulkCopy.DestinationTableName
        //     property of the System.Data.SqlClient.SqlBulkCopy object.
        //
        // Parameters:
        //   table:
        //     A System.Data.DataTable whose rows will be copied to the destination table.
        //
        //   rowState:
        //     A value from the System.Data.DataRowState enumeration. Only rows matching
        //     the row state are copied to the destination.
        public void WriteToServer(DataTable table, DataRowState rowState);

If you want to limit it to only rows without NULL values, I suppose you could set that up in the rowState before calling the WriteToServer method.

or, copy rows explicitly:

//
        // Summary:
        //     Copies all rows from the supplied System.Data.DataRow array to a destination
        //     table specified by the System.Data.SqlClient.SqlBulkCopy.DestinationTableName
        //     property of the System.Data.SqlClient.SqlBulkCopy object.
        //
        // Parameters:
        //   rows:
        //     An array of System.Data.DataRow objects that will be copied to the destination
        //     table.
        public void WriteToServer(DataRow[] rows);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.