using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public byte [] rec1;
public object rec;
public int a;
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
string aa = "Data Source=(local);Initial Catalog=ab;Integrated Security=True";
SqlConnection con = new SqlConnection();
con.ConnectionString = aa;
string cmdText = "SELECT * FROM 温湿度 where 时间='" + textBox2.Text + "'";
//MessageBox.Show(cmdText);
SqlDataAdapter da = new SqlDataAdapter(cmdText, con);
DataSet ds = new DataSet();
try
{
con.Open();
da.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
textBox1.Text = textBox2.Text;
textBox3.Text = row["温度"].ToString();
textBox4.Text = row["湿度"].ToString();
}
}
catch (Exception)
{
MessageBox.Show("对不起!查询失败!", "提示");
}
finally
{
con.Close();
}
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
string aa = "Data Source=(local);Initial Catalog=ab;Integrated Security=True";
SqlConnection con = new SqlConnection();
con.ConnectionString = aa;
string cmdText = "SELECT * FROM 温湿度";
SqlDataAdapter da = new SqlDataAdapter(cmdText, con);
DataSet ds = new DataSet();
try
{
con.Open();
da.Fill(ds);
}
catch (Exception)
{
MessageBox.Show("对不起!查询失败!", "提示");
}
finally
{
con.Close();
}
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
string aa = "Data Source=(local);Initial Catalog=ab;Integrated Security=True";
SqlConnection con = new SqlConnection();
con.ConnectionString = aa;
string cmdText = "insert into 温湿度(时间,温度,湿度) values ('"+textBox1.Text +"','"+textBox3.Text+"','"+textBox4.Text +"')";
SqlCommand cmd = new SqlCommand(cmdText ,con);
try
{
con.Open();
cmd.ExecuteNonQuery();
qingkong();
}
catch (Exception)
{
MessageBox.Show("对不起!保存数据失败!", "提示");
}
finally
{
con.Close();
}
shuaxin();
}
void qingkong()
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
}
void shuaxin()
{
string aa = "Data Source=(local);Initial Catalog=ab;Integrated Security=True";
SqlConnection con = new SqlConnection();
con.ConnectionString = aa;
string cmdText = "SELECT * FROM 温湿度";
SqlDataAdapter da = new SqlDataAdapter(cmdText, con);
DataSet ds = new DataSet();
try
{
con.Open();
da.Fill(ds);
}
catch (Exception)
{
MessageBox.Show("对不起!查询失败!", "提示");
}
finally
{
con.Close();
}
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
private void axMSComm1_OnComm(object sender, EventArgs e)
{
//byte[] rec1;
//if (axMSComm1.CommEvent== ComEvReceive)
//axMSComm1.CommEvent == 1;
//if (axMSComm1.CommEvent.Equals(1))
//rec= Convert.ToByte(axMSComm1.Input);
//c =axMSComm1.Input;
//b = (byte)rec;
timer1.Enabled = true;
rec= axMSComm1.Input;
rec1= (byte[] )rec;
rec1[0] = (byte)(rec1[0] - 100);
rec1[1] = (byte)(rec1[1] - 200);
textBox5.Text = Convert.ToString(rec1[0])+"℃";
textBox6.Text = Convert.ToString(rec1[1])+"%";
listBox1.Items.Add(textBox5.Text);
textBox7.Text =Convert.ToString (DateTime.Now);
//textBox7.Text = Convert.ToString(a);
// a++;
}
private void button2_Click(object sender, EventArgs e)
{
axMSComm1.InBufferCount = 0;
axMSComm1.PortOpen = true;
//axMSComm1.Output = 0x12;
if (axMSComm1.PortOpen==true)
MessageBox.Show("串口已打开!", "提示");
//timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
axMSComm1.PortOpen = false;
//textBox7.Text=DateTime.Now.ToString();
//textBox5.Text = "12";
//textBox6.Text = "162";
string aa = "Data Source=(local);Initial Catalog=ab;Integrated Security=True";
SqlConnection con = new SqlConnection();
con.ConnectionString = aa;
string cmdText = "insert into 温湿度(时间,温度,湿度) values ('" + textBox7.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')";
SqlCommand cmd = new SqlCommand(cmdText, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
qingkong();
}
catch (Exception)
{
timer1.Enabled = false;
MessageBox.Show("对不起!保存数据失败!", "提示");
}
finally
{
con.Close();
}
shuaxin();
timer1.Enabled = false;
axMSComm1.PortOpen = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void groupBox3_Enter(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
axMSComm1.PortOpen = false;
timer1.Enabled = false;
}
}
}
邓凌佳
- 粉丝: 79
- 资源: 1万+
最新资源
- 基于Simulink的考虑局部遮阴的光伏PSO-MPPT控制模型.rar
- 基于Simulink的最大功率点追踪MPPT功能的单相单级脉宽调制(PWM)光伏逆变器,并且支持并网运行.rar
- 基于TCN-GRU的自行车租赁数量预测研究Matlab代码.rar
- 基于TCN-GRU-Attention的自行车租赁数量预测研究Matlab代码.rar
- 基于WoodandBerry1和非耦合控制WoodandBerry2来实现控制木材和浆果蒸馏柱控制Simulink仿真.rar
- 基于变分多谐波对偶模式追踪从噪声信号中提取重复瞬态分量的方法附Matlab代码.rar
- 基于Python的智能门禁打卡系统设计与开发-含详细代码及解释
- 数电课件,数字电路与逻辑
- A Neural Probabilistic Language Model.pdf
- 基于Java的学生信息管理系统实现
- OpenCV人脸检测和识别
- 管理工具PKIManager-1.1.3.6-全算法版本-信创
- ACM程序设计经典题目与解决方案(C语言实现)
- 详细的Visual Studio安装教程及注意事项
- 手机侧面轮廓尺寸检测机3D图纸和工程图机械结构设计图纸和其它技术资料和技术方案非常好100%好用.zip
- GitHub教程:账号注册、项目创建与协同开发详解
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈