将Excel电子表格中的位类型导入SQL Server

时间:2016-12-13 18:33:50

标签: sql-server excel types oledb

我有一个电子表格,其中一列是IsActive,所有值都是1(Excel中的数字数据类型)。在SQL Server中,其插入的表具有IsActive的位数据类型。但是,尝试插入数据时出现的错误是

The given value of type String from the data source cannot be converted to type bit of the specified target column.

Excel中的数据类型是否与SQL Server中的某个位兼容?

 excelConnection.Open();
                    using (OleDbDataReader dReader = cmd.ExecuteReader())
                    {
                        using (SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
                        {
                            //Give your Destination table name 
                            sqlBulk.DestinationTableName = "Medicine";
                            sqlBulk.WriteToServer(dReader);
                        }
                    }

编辑:这是OleDB命令

 using (OleDbCommand cmd = new OleDbCommand("Select ...[IsActive]... from [Sheet1$]", excelConnection)

我显然不希望将字符串IsActive插入位列。

1 个答案:

答案 0 :(得分:0)

using (OleDbCommand cmd = new OleDbCommand("Select ...CAST(COALESCE([IsActive], 0) AS BIT)... from [Sheet1$]", excelConnection)