Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 708
Yang You Avatar asked May 10 '16 05:05

Yang You


People also ask

How do I add a column to a bit in SQL?

If a table has up to 8 BIT columns, the columns are stored as 1 byte, if there are more than 8 and up to 16 BIT columns, SQL Server stores the BIT columns as 2 bytes, and so on. To create a table with BIT column: CREATE TABLE table_name ( bit_column BIT );

What is bit value in SQL?

SQL Row_Number. SQL TinyInt. The BIT data type is an integer value that accepts 0, 1, and NULL. BIT represents a boolean type with TRUE (1) and FALSE (0) values. String values 'TRUE' and 'FALSE' are also accepted and converted to 1 and 0.

How do I load a flat file in SQL Server?

To access the Import Flat File Wizard, follow these steps: Open SQL Server Management Studio. Connect to an instance of the SQL Server Database Engine or localhost. Expand Databases, right-click a database (test in the example below), point to Tasks, and click Import Flat File above Import Data.


1 Answers

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

like image 137
TechnicalTophat Avatar answered Nov 03 '22 10:11

TechnicalTophat