3

I am taking data from an MS Access database and moving it to SQL Server using this code in my console application:

if (getvalue == "plantname")
{
        Console.WriteLine("NetWeightMasterData-Plant Started");
        var dw = new System.Data.DataTable();
        string accdbConnStriok = ConfigurationManager.ConnectionStrings["NetWeightIok"].ToString();
        using (var accdbConn = new OdbcConnection(accdbConnStriok))
        {
       using (var da = new OdbcDataAdapter("SELECT [Unit UPC Base Item],
    [Production Line],[Preset Number],[Package Type],[Weight Factor],
    [Piece],[Pcs Per Unit],[Upper Limit Unit],[Upper Limit Factor],
    [Label Wt (g)],[Tare Wt (g)],[Constant Tare Wt (g)],
    [Tare Variation Factor (g)],[Pkg Length (mm)],[Film Product Code],
    [Film Width (mm)],[Forming Tube (mm)],[Type of Jaws],[Last Updated],
    [Comments] FROM [Net Weight Master Data]", accdbConn))
           {
             da.Fill(dw);

           }
        }
    Console.WriteLine("DataTable filled from NetWeight db NetWeightMasterData-Iola- Row count: {0}", dw.Rows.Count, DateTime.Now.ToString());
    string sqlConnStriok = ConfigurationManager.ConnectionStrings["sqlconiok"].ToString();
     using (var sqlConn = new SqlConnection(sqlConnStriok))
       {
         sqlConn.Open();
         using (var cmd = new SqlCommand())
       {
          cmd.Connection = sqlConn;
         cmd.CommandText = "CREATE TABLE #NetWeightMasterData ([Unit UPC Base Item] [nvarchar](50) NOT NULL,[Production Line] [nvarchar](50) NOT NULL,
    [Preset Number] [nvarchar](50) NULL,[Package Type] [nvarchar](50) NULL,
    [Weight Factor] [float](53) NULL,[Piece] [nvarchar](255),
    [Pcs Per Unit] [float](53) NULL,[Upper Limit Unit] [nvarchar](50) NULL,
    [Upper Limit Factor] [float](53) NULL,[Label Wt (g)] [float](53) NULL,
    [Tare Wt (g)] [float](53) NULL,[Constant Tare Wt (g)] [float](53) NULL,
    [Tare Variation Factor (g)] [float](53) NULL,
    [Pkg Length (mm)] [float](53) NULL,[Film Product Code] [nvarchar](255) NULL,
    [Film Width (mm)] [int] NULL,[Forming Tube (mm)] [int] NULL,
    [Type of Jaws] [nvarchar](255) NULL,[Last Updated] [datetime] NULL,
    [Comments] [nvarchar](255) 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 = "#NetWeightMasterData";                                       Console.WriteLine(DateTime.Now.ToString());
        sbc.WriteToServer(dw);
        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 #NetWeightMasterData";
           Console.WriteLine("SqlBulkCopy complete. Temp table row count: {0}", cmd.ExecuteScalar());
         cmd.CommandText = "TRUNCATE TABLE [dbo].[Net Weight Master Data]";
        cmd.ExecuteNonQuery();
        Console.WriteLine("Truncated NetWeightMasterDataTable");
     cmd.CommandText = "INSERT INTO [dbo].[Net Weight Master Data] 
([Unit UPC Base Item],[Production Line],[Preset Number],[Package Type],
    [Weight Factor],[Piece],[Pcs Per Unit],[Upper Limit Unit],
    [Upper Limit Factor],[Label Wt (g)],[Tare Wt (g)],
    [Constant Tare Wt (g)],[Tare Variation Factor (g)],
    [Pkg Length (mm)],[Film Product Code],[Film Width (mm)],
    [Forming Tube (mm)],[Type of Jaws],[Last Updated],
    [Comments]) SELECT Z.[Unit UPC Base Item],Z.[Production Line],
    Z.[Preset Number],Z.[Package Type],Z.[Weight Factor],
    Z.[Piece],Z.[Pcs Per Unit],Z.[Upper Limit Unit],
    Z.[Upper Limit Factor],Z.[Label Wt (g)],Z.[Tare Wt (g)],
    Z.[Constant Tare Wt (g)],Z.[Tare Variation Factor (g)],
    Z.[Pkg Length (mm)],Z.[Film Product Code],Z.[Film Width (mm)],
    Z.[Forming Tube (mm)],Z.[Type of Jaws],Z.[Last Updated],
    Z.[Comments] FROM #NetWeightMasterData Z";
        Console.WriteLine(DateTime.Now.ToString());
        cmd.ExecuteNonQuery();
        cmd.CommandText = "SELECT COUNT(*) AS m FROM [dbo].[Net Weight Master Data]";
       Console.WriteLine("Inserted Records into NetWeightMasterData:{0}", cmd.ExecuteScalar());
    }
     tran.Commit();
     Console.WriteLine("do you wann to insert NetWeightMasterData data into another plant please type y for yes and n for no");
     string answer = Console.ReadLine();
        if (answer == "y")
        {
       LoadNetWeightMasterData();
        }
       else
        {
        LoadNetWeightTracking();
        }
       }
       }
       }

My Access table has some empty dates in it but in my SQL Server table I let it allow nulls but I am still getting the error

SqlDateTime Overflow must be between 1/1/1753 and 12/31/9999

I realize that this error also comes up when the date is not the right format but I checked that too and it all looks good, so what else am I missing?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ExpertWannaBe
  • 117
  • 3
  • 17

2 Answers2

2
  1. Suggestion 1: If you want to ensure every row is in the correct SQL Server Date Time format, you could write a foreach in your datatable after it was filled with data from AccessDb and check if it's smaller then 1753/1/1... if it's true, assign a valid value (null or GETDATE()).
  2. Suggestion 2: You can update your datarows in c# with the property System.Data.SqlTypes.SqlDateTime.MinValue. (Check it out here: https://msdn.microsoft.com/en-us/library/system.data.sqltypes.sqldatetime.minvalue(v=vs.110).aspx)
Kevy Granero
  • 643
  • 5
  • 17
  • Thanks Bud, instead of all that i went to access and did a sort oldest to newest and i was shocked to find this date in the column 11/1/1414 :) so i had to replace the year with something i could work with and the data load was successful – ExpertWannaBe Aug 14 '15 at 21:58
  • @ExpertWannaBe glad you've found a solution and could move forward. – Kevy Granero Jun 22 '20 at 01:29
0

The lower bound on access is lower than the one in sql.
See here: Date Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM OverFlow error SqlBulkCopy

Community
  • 1
  • 1
EAnders
  • 112
  • 8