【问题标题】:Data from excel to sql tabel is importing in NULL values...why??从 excel 到 sql 表的数据以 NULL 值导入...为什么?
【发布时间】:2013-06-01 08:21:30
【问题描述】:

我正在将数据从 excel 导入数据库。 以下是 excel 列的架构: 下面是 db 的架构......并且与 excel 工作表数据完全匹配............

ID  integer                 
organizationId  integer             
categoryId  integer         
attribute   text          
Y1960       integer or null     
Y1961       integer or null      
Y1962       integer or null       
Y1963       integer or null      
Y1964       integer or null     
Y1965       integer or null       
Y1966       integer or null      
Y1967       integer or null       
Y1968       integer or null      
Y1969       integer or null

数据库列也是如此。我正在使用以下代码检索 first 10 columns from excel 文件的数据。

OleDbConnection excelConnection =
                        new OleDbConnection(excelConnectionString);

        //Create OleDbCommand to fetch data from Excel
        OleDbCommand cmd = new OleDbCommand
        ("Select [ID],[organizationID],[categoryID],[parentID],[granularityLevel],[attribute],[Y1960],[Y1961],[Y1962],[Y1963],[Y1964],[Y1965] from [Details$]", excelConnection);

        excelConnection.Open();
        OleDbDataReader dReader;
        dReader = cmd.ExecuteReader();

        SqlBulkCopy sqlBulk = new SqlBulkCopy(connectionString);
        sqlBulk.DestinationTableName = "Data";
        sqlBulk.WriteToServer(dReader);

问题是Y1960 column is not copied 的数据从 excel 文件到数据库表中。 复制其他列,但不复制 Y1960。那里只有空值可用。

【问题讨论】:

    标签: c# sql excel


    【解决方案1】:

    过去我遇到过几次这个问题,我找到的解决方案是将目标数据类型设为浮点数。这里有一个建议是先暂存数据,然后进行转换,这也可以是一个选项Error converting data types when importing from Excel to SQL Server 2008

    【讨论】:

      【解决方案2】:

      我希望Y1960 是您的 Excel 表格的第一行。

      当您当时执行此复制任务时first excel sheet row take as title of column 这就是不应复制此行的原因。您必须在工作表顶部添加多行标题才能尝试。

      【讨论】:

      • @asim 什么是 id ?你能给我详细介绍一下你的 Excel 表格格式吗?
      【解决方案3】:

      我遇到了同样的问题,两列没有被正确复制,而是插入了空值。解决我的问题的方法是在扩展属性中设置 HDR=NO。 将所有列设为 Varchar。它适用于我的场景,因为我可以在 SQL 中作为 VC 做任何事情

      谢谢, TP

      【讨论】:

        【解决方案4】:

        当我的混合数据从 excel 移动到 Sql 表时,它显示为 Null。要在 excel 的连接字符串中解决此添加 IMEX 选项。它告诉驱动程序始终将“混合”数据列作为文本读取。

        Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\MyExcel.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-07
          • 2013-03-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多