namespace TRSQL
{
public partial class Form1 : Form
{
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private ImageList imagelst;
public SqlDataReader dr1;
public Form1()
{
imagelst = new ImageList();
InitializeComponent();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = 1000;
}
private void timer_Tick(object sender, EventArgs e)
{
timer.Enabled = false;
button1.PerformClick();
}
private void button1_Click(object sender, EventArgs e)
{
chart1.Series["Series1"].Points.Clear();
SqlConnection con = new SqlConnection(@"server=HIST;database=PLC_DB;uid=sa;password=NCADMIN@123;MultipleActiveResultSets=True");
con.Open();
SqlCommand cmd = new SqlCommand("select * from [PLC_DB].[dbo].[EVEREST] WHERE DATESTR BETWEEN '" + dateTimePicker1.Value.ToString("yyyy/MM/dd ") + "' AND '" + dateTimePicker2.Value.ToString("yyyy/MM/dd") + "'", con);
cmd.ExecuteNonQuery();
dr1 = cmd.ExecuteReader();
while (dr1.Read())
{
chart1.Series["Series1"].Points.Clear();
chart1.Series["Series1"].Color = Color.Red;
chart1.Series["Series1"].Points.AddXY(dr1["DATESTR"].ToString(), dr1["Value"].ToString());
chart1.ChartAreas[0].AxisX.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.True;
chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
//chart1.Update();
////chart1.Series.IsValueShownAsLabel = true;
chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
chart1.Series["Series1"].IsValueShownAsLabel = true;
chart1.ChartAreas[0].AxisY.Maximum = 250;
chart1.ChartAreas[0].AxisY.Minimum = 200;
}
con.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
chart1.Series["Series1"].Points.Clear();
chart1.Series["Series2"].Points.Clear();
string selected = comboBox1.Text;
// label1.Text = dateTimePicker1.Value.ToString("yyyy/MM/dd");
SqlConnection con = new SqlConnection(@"server=HIST;database=PLC_DB;uid=sa;password=NCADMIN@123;MultipleActiveResultSets=True");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT top (10) * FROM [PLC_DB].[dbo].[EVEREST] WHERE [DATESTR] > DATEADD(HOUR, - " + selected + ", GETDATE())", con);
cmd.ExecuteNonQuery();
dr1 = cmd.ExecuteReader();
while (dr1.Read())
{
chart1.Series["Series1"].Points.AddXY(dr1["Tag1"].ToString(), dr1["TagValue1"].ToString());
chart1.Series["Series2"].Points.AddXY(dr1["Tag2"].ToString(), dr1["TagValue2"].ToString());
chart1.ChartAreas[0].AxisX.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.True;
chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Series[1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Series["Series1"].IsValueShownAsLabel = true;
chart1.Series["Series2"].IsValueShownAsLabel = true;
//chart1.Series["Series2"].Label
}
con.Close();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
dateTimePicker1 = new DateTimePicker();
dateTimePicker1.Format = DateTimePickerFormat.Time;
dateTimePicker1.ShowUpDown = true;
}
private void button2_Click(object sender, EventArgs e)
{
chart1.Series["Series1"].Points.Clear();
string selected = comboBox1.Text;
// label1.Text = dateTimePicker1.Value.ToString("yyyy/MM/dd");
SqlConnection con = new SqlConnection(@"server=HIST;database=PLC_DB;uid=sa;password=NCADMIN@123;MultipleActiveResultSets=True");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM [PLC_DB].[dbo].[TEMP] WHERE [DATESTR] > DATEADD(HOUR, - " + selected + ", GETDATE())", con);
cmd.ExecuteNonQuery();
dr1 = cmd.ExecuteReader();
while (dr1.Read())
{
chart1.Series["Series1"].Points.AddXY(dr1["Tag"].ToString(), dr1["TagValue"].ToString());
chart1.Series["Series2"].Points.AddXY(dr1["Tag"].ToString(), dr1["TagValue"].ToString());
chart1.ChartAreas[0].AxisX.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.True;
chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Series[1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Series["Series1"].IsValueShownAsLabel = true;
chart1.ChartAreas[0].AxisY.Maximum = 250;
chart1.ChartAreas[0].AxisY.Minimum = 200;
}
con.Close();
}
}
}
我正在使用c #windows窗体,我必须显示sql server数据在不同日期和时间进入的趋势。有两个参数,一个是DATESTR,它在X轴上,第二个是在Y轴上的值。工具提示仅显示Y轴值,但无法显示X轴值。在图表中,它显示0作为x轴的值。