SqlDateTime 溢出必须介于 1/1/1753 和 12/31/9999 之间,来自对 sql 的访问
SqlDateTime Overflow must be between 1/1/1753 and 12/31/9999 coming from access to sql
我正在从 MS Access 数据库中获取数据并将其移动到 SQL 服务器,在我的控制台应用程序中使用以下代码:
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();
}
}
}
}
我的访问 table 中有一些空日期,但在我的 SQL 服务器中 table 我让它允许空值,但我仍然收到错误
SqlDateTime Overflow must be between 1/1/1753 and 12/31/9999
我意识到当日期格式不正确时也会出现此错误,但我也检查了一下,一切看起来都不错,所以我还缺少什么?
访问权限下限低于sql。
看这里:
- 建议1:如果你想确保每一行都是正确的SQL服务器日期时间格式,你可以在数据表中填充来自AccessDb的数据后在数据表中写一个foreach并检查它是否更小然后 1753/1/1... 如果为真,则分配一个有效值(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)
我正在从 MS Access 数据库中获取数据并将其移动到 SQL 服务器,在我的控制台应用程序中使用以下代码:
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();
}
}
}
}
我的访问 table 中有一些空日期,但在我的 SQL 服务器中 table 我让它允许空值,但我仍然收到错误
SqlDateTime Overflow must be between 1/1/1753 and 12/31/9999
我意识到当日期格式不正确时也会出现此错误,但我也检查了一下,一切看起来都不错,所以我还缺少什么?
访问权限下限低于sql。
看这里:
- 建议1:如果你想确保每一行都是正确的SQL服务器日期时间格式,你可以在数据表中填充来自AccessDb的数据后在数据表中写一个foreach并检查它是否更小然后 1753/1/1... 如果为真,则分配一个有效值(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)