由于数据库中DateTime类型字段,最小值是1/1/1753 12:00:00,而.NET Framework中,DateTime类型,最小值是1/1/0001 0:00:00,显然,超出了sql的值的最小值范围,导致数据溢出错误
using System.Data.SqlTypes; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine(".net最小时间为{0}",DateTime.MinValue.ToString()); Console.WriteLine(".net最大时间为{0}",DateTime.MaxValue.ToString()); Console.WriteLine("SQL最小时间为{0}",SqlDateTime.MinValue.ToString()); Console.WriteLine("SQL最大时间为{0}",SqlDateTime.MaxValue.ToSqlString()); Console.ReadKey(); } } }
try { using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand()) { cmd.Connection = sqlConn; cmd.CommandType = CommandType.Text; cmd.CommandText = "insert into tCollectionLog (iID_tControlNodes, cNodeName, cValue, cQuality, iType, cReturnInfo, dOPC)values(@iID_tControlNodes, @cNodeName, @cValue, @cQuality, @iType, @cReturnInfo, @dOPC)"; cmd.Parameters.AddWithValue("@iID_tControlNodes", iID_tControlNodes); cmd.Parameters.AddWithValue("@cNodeName", item.ItemName); cmd.Parameters.AddWithValue("@cValue", item.Value == null ? DBNull.Value : item.Value);//没有值时出错;参数化查询 '(@iID_tControlNodes nvarchar(36),@cNodeName nvarchar(13),@cValue' 需要参数 '@cValue',但未提供该参数。 cmd.Parameters.AddWithValue("@cQuality", string.Format("{0}", item.Quality)); cmd.Parameters.AddWithValue("@iType", type == OPCDataType.CURRENTTAG ? 1:2); cmd.Parameters.AddWithValue("@cReturnInfo", item.ResultID.ToString()); cmd.Parameters.AddWithValue("@dOPC", item.Quality == Quality.Bad ? DBNull.Value : (object)item.Timestamp); cmd.ExecuteNonQuery(); } } catch(Exception ex) { IoriUtilityLib.Iori.LogUtil.logError(string.Format("InsertOPCReadLog:{0}" , ex.Message)); }
标签:00,12,59,Parameters,cmd,item,AddWithValue,Console From: https://www.cnblogs.com/ioriwellings/p/17823592.html