之前用sunnyui做展示数据库数据曲线的时候、偶然会报溢出错误,也不报错错误在哪,就是直接程序都跑不动了。
后面发现
设置曲线上下限的时候,当上下限一样的时候就会导致溢出错误、sunnyui的曲线也没有对其抛出异常
对其maxTime和minTime进行数据处理就能解决这个问题了
曲线就可以正常绘制了
private void writetime_Tick(object sender, EventArgs e)
{
select_t();
if (Con.State == System.Data.ConnectionState.Closed)
{
Con.Open();
// 连接已打开后的操作
}
foreach (var i in lines)
{
SqlDataAdapter cm = new SqlDataAdapter($"select Temperature,Machine,Time from UT_TemperatureControl where Machine='{i.Name}' ", Con);//and DATEDIFF(day,Time,GETDATE())=0
//SqlCommand b = new SqlCommand($"select count(*) from tt where Machine={i.Name}", Con);
//int k=(int)b.ExecuteScalar();
SqlCommand firstRecordCmd = new SqlCommand("SELECT TOP 1 Time FROM UT_TemperatureControl ORDER BY Time ASC", Con);//where DATEDIFF(day,Time,GETDATE())=0
SqlCommand lastRecordCmd = new SqlCommand("SELECT TOP 1 Time FROM UT_TemperatureControl ORDER BY Time DESC", Con);//where DATEDIFF(day,Time,GETDATE())=0
object firstRecordTimeObj = firstRecordCmd.ExecuteScalar();
object lastRecordTimeObj = lastRecordCmd.ExecuteScalar();
DateTime minTime;
DateTime maxTime;
if (firstRecordTimeObj != null && firstRecordTimeObj != DBNull.Value)
{
if (Convert.ToDateTime(firstRecordTimeObj) == Convert.ToDateTime(lastRecordTimeObj))
{
minTime = Convert.ToDateTime(firstRecordTimeObj).AddHours(-1);
maxTime = Convert.ToDateTime(lastRecordTimeObj).AddHours(1);
}
else
{
minTime = Convert.ToDateTime(firstRecordTimeObj);
maxTime = Convert.ToDateTime(lastRecordTimeObj);
}
}
else
{
// 处理无记录的情况,设置一个默认的最小时间,或者抛出异常,根据实际情况决定
minTime = DateTime.MinValue;
maxTime = DateTime.Now;
}
float temperature = 0.0f;
DateTime currentTime = DateTime.Now;
//double ctime = currentTime.Ticks;
DataTable dt = new DataTable();
cm.Fill(dt);
//var line = historylc.Option.AddSeries(new UILineSeries(i.Name));
historylc.Option.Clear(i.Name);
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
{
if (col.ColumnName == "Temperature")
{
temperature = Convert.ToSingle(row["Temperature"]);
}
// temperature = (float)row["Temperature"];
if (col.ColumnName == "Time")
{
currentTime = Convert.ToDateTime(row["Time"]);
//string cc = currentTime.ToString("HHmmss");
//ctime = cc.ToDouble();
}
}
historylc.Option.XAxis.SetRange(minTime, maxTime);
historylc.Option.AddData(i.Name, currentTime, temperature);//创建线
//line.Add(currentTime, temperature);
}
}
historylc.Refresh();
if (Con.State == System.Data.ConnectionState.Open)
{
// 连接已打开,进行操作
// ...
Con.Close();
// 连接已关闭后的操作
}
}
标签:Convert,Sunnyui,firstRecordTimeObj,曲线,DateTime,ToDateTime,Time,溢出,Con
From: https://www.cnblogs.com/gho13954/p/18020769