SqlBulkCopy拒绝将String.Empty转换为INT NULL

ado.net c# sqlbulkcopy sql-server-2005

最近,我的任务是创建一个自动ETL过程,通过读取主映射文件,根据平面文件名将数据泵入表中。我决定使用SqlBulkCopy,一切似乎都很好。实现了IDataReader接口来读取平面文件,SQL Server的元数据提供了多列用于一对一数据映射,一切正常,直到我遇到携带空字符串的文件。 SqlBulkCopy抛出一个Exception,指出“数据源中String类型的给定值不能转换为指定目标列的int类型。”故事结束时,它甚至不关心此列的DB类型是INT NULL。我知道我可以进一步解释元数据,提取给定列的数据类型,根据提取的信息构建DataSet,从平面文件重新构建数据并为自己提供一个很好的强类型解决方案,但我是一个懒惰的家伙,如果有人知道解决我突然出现的问题的话,感觉他的幸福被微软恶毒,或者我自己的无能。感谢您的时间。

List<String> fileNames;

DateTime startJobTime = DateTime.Now;

Console.WriteLine("---------------------------------------------");
Console.WriteLine("Start Time: " + startJobTime);
Console.WriteLine("---------------------------------------------");

using (SqlConnection sqlCon = new SqlConnection(sqlConnection))
{
    try
    {
        sqlCon.Open();
        sqlCon.ChangeDatabase(edwDBName);

        // Get service information for staging job
        UnivStage us = GetStagingJobInfo(jobName, sqlCon);
        us.StartJobTime = startJobTime;

         // Get a list of file names
         fileNames = GetFileList(us, args);

         if (fileNames.Count > 0)
         {
              // Truncate Staging Table
              TruncateStagingTable(us, sqlCon);
              // Close and dispose of sqlCon2 connection
              sqlCon.Close();

              Console.WriteLine("Processing files: ");
              foreach (String fileName in fileNames)
                  Console.WriteLine(fileName);
              Console.WriteLine();
          }
          else
          {
              Console.WriteLine("No files to process.");
              Environment.Exit(0);
          }

          // Re-open Sql Connection
          sqlCon.Open();

          sqlCon.ChangeDatabase(stagingDBName);


          foreach (String filePath in fileNames)
          {
               using (SqlTransaction sqlTran = sqlCon.BeginTransaction())
               {
                   using (FlatFileReader ffReader = new FlatFileReader(filePath, us.Delimiter))
                   {
                       using (SqlBulkCopy sqlBulkCopy =
                                new SqlBulkCopy(sqlCon, SqlBulkCopyOptions.Default, sqlTran))
                       {
                            SqlConnection sqlCon2 = new SqlConnection(sqlConnection);

                            SetColumnList(sqlCon2, us, sqlBulkCopy);

                            sqlBulkCopy.BatchSize = 1000;
                            sqlBulkCopy.DestinationTableName =
                                us.StagingSchemaName + "." + us.StagingTableName;

                            sqlBulkCopy.WriteToServer(ffReader);

                            sqlTran.Commit();

                            sqlCon2.Close();
                        }
                    }
                }
            }

            sqlCon.ChangeDatabase(edwDBName);
            sqlCon.Close();

            sqlCon.Open();
            SetRowCount(us, sqlCon);
            sqlCon.Close();

            us.EndJobTime = DateTime.Now;
            sqlCon.Open();
            LogStagingProcess(us, sqlCon);
            sqlCon.Close();

            Console.WriteLine(us.ProcessedRowCount + " rows inserted.");

            Console.WriteLine("---------------------------------------------");
            Console.WriteLine("Success! End Time: " + us.EndJobTime);
            Console.WriteLine("---------------------------------------------");

            Console.ReadLine();
        }
        catch (SqlException e)
        {
            RenderExceptionMessagesAndExit(e,
                "Exception have occured during an attempt to utilize SqlBulkCopy\n");
        }
    }

热门答案

将空字符串转换为DBNull




许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因