SQL Server - can't import value 0 or 1 to bit column

c# csv sqlbulkcopy sql-server sql-server-2014

Question

This seems like a duplicate question. But as I searched & viewed the related questions & answers, i still can't find the answer.

I am writing a loader program which imports data records from CSV file to a database table. Here is the demo.

The database table is something as below:

use Test;
create table BoolTest (Name varchar(20) not null, IsValid bit not null);
insert into BoolTest (Name, IsValid) values('xx', 1);
insert into BoolTest (Name, IsValid) values('yy', 0);

The loader program is like:

class Program
{
    static void Main(string[] args)
    {
        var csvFileContent = "Name,IsValid\r\naa,true\r\nbb,false";            
        var csvLines = csvFileContent.Split(new String[] { "\r\n" }, StringSplitOptions.None);
        var columnNames = csvLines[0].Split(',');

        var table = new DataTable();
        foreach (var columnName in columnNames)
        {
            table.Columns.Add(new DataColumn(columnName));
        }

        for (var n = 1; n < csvLines.Length; ++n)
        {
            var line = csvLines[n];
            var values = line.Split(',');
            table.Rows.Add(values);
        }

        var connectionString = "Data Source=localhost;Initial Catalog=Test;Integrated Security=True";
        using (var bulkCopy = new SqlBulkCopy(connectionString)) {
            bulkCopy.DestinationTableName = "BoolTest";

            bulkCopy.ColumnMappings.Add("Name", "Name");
            bulkCopy.ColumnMappings.Add("IsValid", "IsValid");

            bulkCopy.WriteToServer(table);
        }

        Console.WriteLine("Done");
        Console.Read();
    }
}

Everything is fine above.

But the problem is the CSV file is from 3rd party and the values for column 'IsValid' is 0 or 1 as below

Name,IsValid
aa,1
bb,0

When the loader program tries to load this CSV file, bulkCopy.WriteToServer(table) will throw out exception

enter image description here

The sql server I am using is Sql server 2014.

Any help will be appreciated. Thanks.

--- update ---

Thanks guys for the solutions. I will try to manually change 0/1 to false/true in loader program. But still want to know whether there is easy fix (like changing settings in SQL Server or SqlBulkCopy) for this issue.

I also tried sql in Sql Server Management Studio like

insert into BoolTest (Name, IsValid) values('aa', 1); -- this works as we expected

insert into BoolTest (Name, IsValid) values('aa', '1'); -- this also works to my surprise

Both works well. But not sure why SQL server rejects the bulk copy when the IsValid value is 0 or 1. And when we show the table data with 'select', the value for IsValid is actually 0 or 1.

1
4
5/11/2016 3:12:15 AM

Accepted Answer

I think the error is caused by trying to push a String (cell value in the DataTable) into a BIT column, as bulk insert is not able to perform implicit conversions ("0" and "1" values into 0/1 BIT values).

One way is to manually define columns along with their actual type:

table.Columns.Add(new DataColumn("Name", typeof(String)));
table.Columns.Add(new DataColumn("IsValid", typeof(bool)));

and the cells construction to something like this:

for (var n = 1; n < csvLines.Length; ++n)
{
    var line = csvLines[n];
    var values = line.Split(',');

    var rowValues = new object[values.Length];
    rowValues[0] = values[0];
    // conversion to integer is required as Boolean will not accept the string
    rowValues[1] = Convert.ToBoolean(Convert.ToInt32(rowValues[1]));
}
1
5/10/2016 5:41:06 AM

Popular Answer

It's because Boolean values get replaced by bit values on insert, to make our lives easier to fix this you just need to create a parsing method for converting bit to bool, simple enough. Should then work



Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow