使用 Sql 批量复制插入到我的末端 sql table 后丢失数据

Missing Data after insert into my end sql table using Sql Bulk Copy

我编写了一个控制台应用程序,它从我的访问 table 中提取数据,然后使用 sql 批量复制到临时文件 sql table 然后从临时文件 table 将其加载到 main sql table 中。该应用程序按预期工作,但它从访问 table 中提取了 26624 行,当它对 WriteToServer 进行批量复制时,它仅复制 24880 行,因此当插入主 sql table 时仅复制 24880行被插入。大约缺少 1744 行,但我确实遇到异常

"SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."

我在访问中有开始时间和停止时间的列,这些列的默认值为数据 = 12/30/1899,但时间会改变。

我用来移动数据的代码:

string sqlConnStrabk = ConfigurationManager.ConnectionStrings["sqlconabk"].ToString();

using (var sqlConn = new SqlConnection(sqlConnStrabk))
{
    sqlConn.Open();
    using (var cmd = new SqlCommand())
    {
        cmd.Connection = sqlConn;
        cmd.CommandText = 
            "CREATE TABLE #NetWeightTracking (" + 
            "[Date] [datetime] NULL," + 
            "[Unit UPC Base Item] [nvarchar](50) NULL," + 
            "[Item (Optional)] [nvarchar](50) NULL," + 
            "[Preset Number] [nvarchar](50) NULL," + 
            "[Product Group] [nvarchar](255) NULL," + 
            "[Shift] [nvarchar](255) NULL," + 
            "[Rotation Code] [nvarchar](255) NULL," + 
            "[BBD] [nvarchar](255) NULL," + 
            "[Operator Name] [nvarchar](255) NULL," + 
            "[Supervisor] [nvarchar](255) NULL," + 
            "[Production Line] [nvarchar](255) NULL," + 
            "[Bagger Number] [float] NULL," + 
            "[Start Time] [datetime] NULL," + 
            "[Stop Time] [datetime] NULL," + 
            "[Under Counts] [float] NULL," + 
            "[Label Wt on Pkg (g)] [float] NULL," + 
            "[Machine Tare Wt (g)] [float] NULL," + 
            "[Actual Tare Wt (g)] [float] NULL," + 
            "[Verify Target Wt (g)] [float] NULL," + 
            "[Total Count (Proper)] [float] NULL," + 
            "[Mean Gross (g)] [float] NULL," + 
            "[Rptd Mean Net (g)] [float] NULL," + 
            "[Std Dev (g)] [float] NULL," + 
            "[Max (g)] [float] NULL," + 
            "[Min (g)] [float] NULL," + 
            "[TNE (g)] [float] NULL," + 
            "[Comments] [nvarchar](50) NULL," + 
            "[Field1] [datetime] NULL," + 
            "[Field2] [datetime] NULL," + 
            "[Field3] [nvarchar](255) NULL," + 
            "[Field4] [nvarchar](255) NULL," + 
            "[Field5] [nvarchar](255) NULL," + 
            "[Field6] [nvarchar](255) NULL," + 
            "[Field7] [nvarchar](255) NULL, " + 
            "[Row] [int] IDENTITY(1,1) NOT NULL" + 
            ")";
        cmd.ExecuteNonQuery();
    }
    using (SqlTransaction tran = sqlConn.BeginTransaction(System.Data.IsolationLevel.ReadCommitted))
    {
        try
        {
            using (var sbc = new SqlBulkCopy(sqlConn, SqlBulkCopyOptions.KeepIdentity, tran))
            {
                sbc.BatchSize = 10;
                sbc.NotifyAfter = 100;
                sbc.BulkCopyTimeout = 1000;
                sbc.DestinationTableName = "#NetWeightTracking";
                Console.WriteLine(DateTime.Now.ToString());
                sbc.WriteToServer(du);

                Console.WriteLine("After Datatable", DateTime.Now.ToString());
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message.ToString());
        }

        Console.WriteLine(DateTime.Now.ToString());

        using (var cmd = new SqlCommand())
        {
            cmd.Connection = sqlConn;
            cmd.Transaction = tran;
            cmd.CommandText = "SELECT COUNT(*) AS n FROM #NetWeightTracking";
            Console.WriteLine("SqlBulkCopy complete. Temp table row count: {0}", cmd.ExecuteScalar());
            cmd.CommandText = "TRUNCATE TABLE [dbo].[Net Weight Tracking]";
            cmd.ExecuteNonQuery();
            Console.WriteLine("Truncated NetWeightTrackingTable");

            cmd.CommandText = 
                "INSERT INTO [dbo].[Net Weight Tracking] (" + 
                "[Date],[Unit UPC Base Item],[Item (Optional)]," + 
                "[Preset Number],[Product Group],[Shift],[Rotation Code]," + 
                "[BBD],[Operator Name],[Supervisor],[Production Line]," + 
                "[Bagger Number],[Start Time],[Stop Time],[Under Counts]," + 
                "[Label Wt on Pkg (g)],[Machine Tare Wt (g)]," + 
                "[Actual Tare Wt (g)],[Verify Target Wt (g)]," + 
                "[Total Count (Proper)],[Mean Gross (g)],[Rptd Mean Net (g)]," + 
                "[Std Dev (g)],[Max (g)],[Min (g)],[TNE (g)],[Comments]," + 
                "[Field1],[Field2],[Field3])  " + 
                "SELECT Z.[Date]," + 
                "Z.[Unit UPC Base Item],Z.[Item (Optional)]," + 
                "Z.[Preset Number],Z.[Product Group],Z.[Shift]," + 
                "Z.[Rotation Code],Z.[BBD],Z.[Operator Name]," + 
                "Z.[Supervisor],Z.[Production Line],Z.[Bagger Number]," + 
                "Z.[Start Time],Z.[Stop Time],Z.[Under Counts]," + 
                "Z.[Label Wt on Pkg (g)],Z.[Machine Tare Wt (g)]," + 
                "Z.[Actual Tare Wt (g)],Z.[Verify Target Wt (g)]," + 
                "Z.[Total Count (Proper)],Z.[Mean Gross (g)]," + 
                "Z.[Rptd Mean Net (g)],Z.[Std Dev (g)]," + 
                "Z.[Max (g)],Z.[Min (g)],Z.[TNE (g)],Z.[Comments]," + 
                "Z.[Field1],Z.[Field2],Z.[Field3] " + 
                "FROM  #NetWeightTracking Z";
            Console.WriteLine(DateTime.Now.ToString());
            cmd.ExecuteNonQuery();
            cmd.CommandText = "SELECT COUNT(*) AS m FROM [dbo].[Net Weight Tracking]";
            Console.WriteLine("Inserted Records into NetWeightTracking:{0}",    cmd.ExecuteScalar());
        }
        tran.Commit();
    }
}

请帮忙!!!而且我不确定如何从我看到的默认 12/30/1899 介于 1/1/1753 - 12/31/9999 之间的其他文章中找出为什么没有插入 1744 行我读过它说如果没有附加日期它将默认日期设置为 1/1/0001 然后我可以理解它超出预期范围因此导致溢出。

您需要查看 Access 中的数据,以及 find/fix 那些错误的列值,或者在导出后在您的 TSQL 中处理它们。调试的一种选择 - 将 temp table 替换为真实的 table,将 datetime 数据类型替换为字符串,进行导出,然后查询 table 以查找错误的值,以便了解数据类型寻找。之后,您可以将其切换回临时 table,但在您的导入中,您应该处理 SELECT 语句中的无效日期时间格式,可能使用 CASE 和 CONVERT/CAST.