Date Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM OverFlow error SqlBulkCopy [duplicate]

本秂侑毒 提交于 2019-12-13 06:40:35

问题


I am reading data from an access db and storing it in a temporary sql table then truncate the main sql table and insert the fresh data set and i am accomplishing that task using the code below but the datetime is giving me issues:

Console.WriteLine("NetWeightTracking-Abilene Started");
            var du = new System.Data.DataTable();
            string accdbConnStrabk = ConfigurationManager.ConnectionStrings["NetWeightAbk"].ToString();
            using (var accdbConn = new OdbcConnection(accdbConnStrabk))
            {
                using (var da = new OdbcDataAdapter("SELECT * FROM [Net Weight Tracking]", accdbConn))
                {
                    da.Fill(du);

                }
            }

            Console.WriteLine("DataTable filled from NetWeight db NetWeightTracking-Abilene - Row count: {0}", du.Rows.Count, DateTime.Now.ToString());

            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.Default, tran))
                        {

                            sbc.BatchSize = 100;
                            sbc.NotifyAfter = 100;
                            sbc.BulkCopyTimeout = 100;
                            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();
                }
            }
        }

The issue is caused by the date column any help will be greatly appreciated

I tried checking the access db to see if there are any null dates but none all the dates are available, i found this solution online:

DateTime.ParseExact (txtPunchDate.Text, "yyyy-MM-dd" , null)

how would i incorporate that with my console application?? Thanks :)


回答1:


Well, MS Access represents its datetime data type as a double:

  • The epoch (zero point) of the MS calendar is 30 December 1899 00:00:00
  • The integer portion of the double is the offset in days from the epoch, and
  • The fractional portion of the double is the fractional part of the day.

Per the specification, the domain of the date portion of an MS Access datetime is

  • lower bound: 1 January 100
  • upper bound: 31 December 9999

And since the domain of a SQL Server datetime is:

  • lower bound: 1 January 1753
  • upper bound: 31 December 9999

any dates in your MS Access database prior to 1 January 1753 are going to cause problems. You need to find the bogus data and fix it. A couple of approaches:

  • In your access database, create a view/query to present the data in a form palatable to SQL Server. Then, bulk load from that into SQL Server.

  • Often, since it's pretty much a foregone conclusion that your source data is dirty/corrupted, when bulk loading data into SQL Server, one will bulk load the source data into a working table where all the columns are nullable, of type varchar types and that has no constraints/keys. Once that's done, then run a stored procedure that does the necessary cleanup and massaging of the data prior to moving it to its proper home.



来源:https://stackoverflow.com/questions/30655603/date-must-be-between-1-1-1753-120000-am-and-12-31-9999-115959-pm-overflow-er

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!