我正在使用控制台应用程序中的以下代码从MS Access数据库中获取数据并将其移至SQL Server:

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


我的Access表中有一些空日期,但是在我的SQL Server表中,我允许它允许为空,但仍然出现错误


  SqlDateTime溢出必须在1/1/1753和12/31/9999之间


我意识到当日期格式不正确时也会出现此错误,但是我也进行了检查,而且看起来一切都很好,那么我还缺少什么呢?

最佳答案

建议1:如果要确保每一行都采用正确的SQL Server日期时间格式,则可以在数据表中填充来自AccessDb的数据后,在数据表中写入foreach,并检查其是否小于1753/1/1 ...如果为true,则分配一个有效值(null或GETDATE())。
建议2:您可以使用属性System.Data.SqlTypes.SqlDateTime.MinValue更新c#中的数据行。 (在此处查看:https://msdn.microsoft.com/en-us/library/system.data.sqltypes.sqldatetime.minvalue(v=vs.110).aspx

09-03 20:56