InvalidOperation类型数据源中的字符串错误

时间:2015-01-27 07:32:05

标签: c#

我遇到了

错误
  

数据源中String类型的给定值无法转换为指定目标列的nvarchar类型。

在下面的代码上。这个代码我从上个月开始使用它的工作,我不确定这是什么原因。请建议

DataTable sheetTable = loadSingleSheet(@"" + var_SourceFilePath + "MBF_Cancel_Temp.xlsx" + "", sheetName);

加载表格如下

private static DataTable loadSingleSheet(string fileName, string sheetName)
    {
        DataTable sheetData = new DataTable();
        using (OleDbConnection conn = returnConnection(fileName))
        {
            conn.Open();
            // retrieve the data using data adapter
            OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "] ", conn);
            sheetAdapter.Fill(sheetData);

            //OleDbCommand oledbcmd = new OleDbCommand("select F1, F2, F3, F4, F7, F8, F9, F10, F11, F12, F13, F14, F15, F5 from [" + sheetName + "] WHERE F5 = '100,000'", conn);
            OleDbCommand oledbcmd = new OleDbCommand("select F1, F2, F3, F4, F5, F6, F7, F8, F9, F10 from [" + sheetName + "] ", conn);
            //OleDbCommand oledbcmd = new OleDbCommand("select NO, LOC, [EFF DATE], [EXP DATE], [NAME], [I#C], [D#O#B], [ADD1], [ADD2], [ADD3], [ADD4], [ADD5], [P#C], [BEN#NAME], [BEN#I#C] from [" + sheetName + "] ", conn);
            //conn.Open();
            OleDbDataReader dr = oledbcmd.ExecuteReader();
            SqlBulkCopy bulkcopy = new SqlBulkCopy(var_MSDBConn);
            bulkcopy.DestinationTableName = "AMB_Cancel_Temp";

            while (dr.Read())
            {
                bulkcopy.WriteToServer(dr);
            }

            conn.Close();

        }
        return sheetData;
    }


private static DataTable loadSingleSheet(string fileName, string sheetName)
{
    DataTable sheetData = new DataTable();
    using (OleDbConnection conn = returnConnection(fileName))
    {
        conn.Open();
        // retrieve the data using data adapter
        OleDbDataAdapter sheetAdapter = new OleDbDataAdapter("select * from [" + sheetName + "] ", conn);
        sheetAdapter.Fill(sheetData);

        //OleDbCommand oledbcmd = new OleDbCommand("select F1, F2, F3, F4, F7, F8, F9, F10, F11, F12, F13, F14, F15, F5 from [" + sheetName + "] WHERE F5 = '100,000'", conn);
        OleDbCommand oledbcmd = new OleDbCommand("select F1, F2, F3, F4, F5, F6, F7, F8, F9, F10 from [" + sheetName + "] ", conn);
        //OleDbCommand oledbcmd = new OleDbCommand("select NO, LOC, [EFF DATE], [EXP DATE], [NAME], [I#C], [D#O#B], [ADD1], [ADD2], [ADD3], [ADD4], [ADD5], [P#C], [BEN#NAME], [BEN#I#C] from [" + sheetName + "] ", conn);
        //conn.Open();
        OleDbDataReader dr = oledbcmd.ExecuteReader();
        SqlBulkCopy bulkcopy = new SqlBulkCopy(var_MSDBConn);
        bulkcopy.DestinationTableName = "AMB_Cancel_Temp";

        while (dr.Read())
        {
            bulkcopy.WriteToServer(dr);
        }

        conn.Close();

    }
    return sheetData;
}

1 个答案:

答案 0 :(得分:0)

如果某个表的列太短而无法存储您要存储的值,则会发生此错误。例如,如果您有一个40个字符的字符串,但您的字段定义为nvarchar(30),则会发生这种情况。但是,由于你没有公布表格的定义,所以无法确定。

解决方案是检查列的大小并确保它们足够大以接受您要输入的值。另一种方法是将传入值的大小截断为列的大小。