SqlDateTime溢出必须在1/1/1753和12/31/9999之间来自访问sql

时间:2015-08-14 21:01:39

标签: c# sql ms-access console-application

我从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之间

我意识到当日期不是正确的格式时也出现了这个错误,但是我也检查了它并且它看起来都很好,所以我还缺少什么呢?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)