日期必须在1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM之间



我正在从access db读取数据并将其存储在临时sql表中,然后截断主sql表并插入新数据集,我正在使用下面的代码完成该任务,但日期时间给了我问题:

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();
                }
            }
        }

问题是由日期列引起的如有任何帮助,不胜感激

我试着检查访问数据库,看看是否有任何空日期,但没有所有的日期是可用的,我发现这个解决方案在线:

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

我该如何将其整合到我的控制台应用程序中??谢谢:)

嗯,MS Access将其datetime数据类型表示为double:

  • MS日历的历元(零点)为30 December 1899 00:00:00
  • double的整数部分是以天为单位的纪元偏移量,
  • double的小数部分是一天的小数部分。

根据规范,MS Access datetime的日期部分的域是

  • 下界:1 January 100
  • 上界:31 December 9999

由于SQL Server datetime的域是:

  • 下限:1 January 1753
  • 上界:31 December 9999

在1753年1月1日之前的MS Access数据库中的任何日期都将导致问题。你需要找到假数据并修复它。两种方法:

  • 在您的access数据库中,创建一个视图/查询,以符合SQL Server的形式呈现数据。然后,批量加载到SQL Server。

  • 通常,因为它几乎是一个可以预见的结论,你的源数据是脏的/损坏的,当批量加载数据到SQL Server时,一个将批量加载源数据到一个工作表中,所有的列都是可空的,类型为varchar类型,没有约束/键。一旦完成,然后运行一个存储过程,在将数据移动到适当的位置之前对数据进行必要的清理和处理。

相关内容

  • 没有找到相关文章

最新更新