wpf做的连接mysql数据库项目——jingyanzongjie
来源:互联网 发布:文化自信 知乎 编辑:程序博客网 时间:2024/06/03 21:36
做的一个小Demo:
实现的功能如下:接收底层硬件发送来的数据,通过串口接收存到数据库,分别发送1,2,3,代表三个人,底层每刷一次RFID,上位机就要根据发送数据判断所来人员是谁,并同时弹出界面,显示该数据插入数据库。
实现步骤:1,本机上安装数据库mysql,建立好表和字段。2.安装连接Mysql的组件MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0。并将MySQLDriverCS.dll引用到项目中去(引用方法,右键项目-》添加-》引用即可)3.开发项目编写
代码实现:1连接串口并能实现数据接收,2.在接受数据同时判断数据并插入数据到相应数据库中 3.设置定时时间,每次接收文本框中信息变化时,利用TextChanged事件:设置定时new出子窗口显示数据库中记录并且定时时间到时将窗口隐藏起来。
另外将窗口居中显示:用WindowStratLocation ="CenterScreen"
关键代码:
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MySQLDriverCS;
using System.Windows.Threading;
using System.IO.Ports;
using System.Timers;
using System.Threading;
using Timer = System.Timers.Timer;
namespace Application1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
///
public partial class MainWindow : Window
{
private DispatcherTimer ShowTimer;
SerialPort mySerialPort = new SerialPort();//定义一个串口实例
string[] portsName = SerialPort.GetPortNames();
// bool flag = true;
Card card2 = new Card();
System.Timers.Timer aTimer = new System.Timers.Timer();//定义定时器用来定时向数据库插入数据
//接收数据并显示函数
string str = null;
string strs = null;
//窗口初始化
public MainWindow()
{
InitializeComponent();
ShowTimer = new System.Windows.Threading.DispatcherTimer();
ShowTimer.Tick += new EventHandler(ShowCurTimer);//起个Timer一直获取当前时间
ShowTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
ShowTimer.Start();
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)//串口数据接收事件
{
MySQLConnection mysqlconn = null;
//this.Dispatcher.BeginInvoke(new Action(() => card.Show()));
//在下面修改所连接数据库的用户名和密码(下面的连接的数据库的用户名是root,密码是123)
//要根据自己安装的数据库设置的用户名和密码来修改,比如你安装Mysql数据库时设置的用户名是root2,密码是123456,
//那么就要把下面的root改成root2,123改为123456即可
mysqlconn = new MySQLConnection(new MySQLConnectionString("localhost", "c#", "root", "123").AsString);
mysqlconn.Open();
strs= mySerialPort.ReadExisting();//字符串方式读//字符串方式读
// this.Dispatcher.BeginInvoke(new Action(() => txtBox1.Text = str));
str = strs.Substring(0, 1);
if (str == "1")
{
this.Dispatcher.BeginInvoke(new Action(() => txtBox1.Text = "张三"));
string str2 = "INSERT INTO card2(id,username,time)VALUES('" + (1) + "','" + "张三" + "','" + (DateTime.Now.ToString()) + "')";//,'" + (DateTime.Now.ToString()) + "'
MySQLCommand mysqlcommand = new MySQLCommand(str2, mysqlconn);
mysqlcommand.ExecuteNonQuery();
}
else if (str == "2")
{
this.Dispatcher.BeginInvoke(new Action(() => txtBox1.Text = "李四"));
string strcon2 = "INSERT INTO card2(id,username,time)VALUES('" + (2) + "','" + "李四" + "','" + (DateTime.Now.ToString()) + "')";//,'" + (DateTime.Now.ToString()) + "'
MySQLCommand mysqlcommand2 = new MySQLCommand(strcon2, mysqlconn);
mysqlcommand2.ExecuteNonQuery();
}
else if (str == "3")
{
this.Dispatcher.BeginInvoke(new Action(() => txtBox1.Text = "王五"));
string strcon3 = "INSERT INTO card2(id,username,time)VALUES('" + (3) + "','" + "王五" + "','" + (DateTime.Now.ToString()) + "')";//,'" + (DateTime.Now.ToString()) + "'
MySQLCommand mysqlcommand3 = new MySQLCommand(strcon3, mysqlconn);
mysqlcommand3.ExecuteNonQuery();
}
mysqlconn.Close();
}
//加载窗口
private void Window_Loaded(object sender, RoutedEventArgs e)
{
foreach (string com in System.IO.Ports.SerialPort.GetPortNames())//自动获取串行口名称
combox1.Items.Add(com);
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
//获取当前时间:成功
#region
public void ShowCurTimer(object sender, EventArgs e)
{
//"星期"+DateTime.Now.DayOfWeek.ToString(("d"))
//获得星期几
this.txtBck1.Text = DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn"));
this.txtBck1.Text += " ";
//获得年月日
this.txtBck1.Text += DateTime.Now.ToString("yyyy年MM月dd日"); //yyyy年MM月dd日
this.txtBck1.Text += " ";
//获得时分秒
this.txtBck1.Text += DateTime.Now.ToString("HH:mm:ss");
}
#endregion
//打开串口的方法:正常
#region
private void OpenSerialPort()
{
if (mySerialPort.IsOpen)
{
btn_Port.Content = "打开串口";
mySerialPort.Close();
MessageBox.Show("串口已关闭");
}
else
{
if (portsName.Length > 0)
{
Array.Sort(portsName);
mySerialPort.PortName = portsName[0];
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
try
{
mySerialPort.Open();
if (mySerialPort.IsOpen)
{
btn_Port.Content = "关闭串口";
MessageBox.Show("已打开");
btn_Port.Tag = "close";
}
}
catch
{
MessageBox.Show("串口被占用!");
return;
}
}
else
{
btn_Port.Tag = "open";
btn_Port.Content = "打开串口";
MessageBox.Show("没有找到串口");
}
}
}
#endregion
//打开串口:正常
private void btn_Port_Click(object sender, RoutedEventArgs e)
{
OpenSerialPort();//打开串口
}
//发送数据正常
#region
private void btn_Open_Click(object sender, RoutedEventArgs e)
{
SendData.Send_Data (0xaa,mySerialPort );
}
private void btn_Close_Click(object sender, RoutedEventArgs e)
{
SendData.Send_Data (0xbb,mySerialPort );
}
public void TimerMange()
{
aTimer.Elapsed += new ElapsedEventHandler(ShowData); //定时事件的方法
aTimer.Interval = 6000;//此处可以设置定时时间,时间到就进入事件处理
aTimer.Enabled = false;
}
public void ShowData(object sender, EventArgs e)
{
this.Dispatcher.BeginInvoke(new Action(() => card2.Hide()));
}
//Text内容改变触发事件
private void txtBox1_TextChanged(object sender, TextChangedEventArgs e)
{
////MySQLConnection mysqlconn = null;
//////this.Dispatcher.BeginInvoke(new Action(() => card.Show()));
//////在下面修改所连接数据库的用户名和密码(下面的连接的数据库的用户名是root,密码是123)
//////要根据自己安装的数据库设置的用户名和密码来修改,比如你安装Mysql数据库时设置的用户名是root2,密码是123456,
//////那么就要把下面的root改成root2,123改为123456即可
////mysqlconn = new MySQLConnection(new MySQLConnectionString("localhost", "c#", "root", "123").AsString);
////mysqlconn.Open();
try
{
Card card2 = new Card();//这一句很重要,,,每次重新new一个出来,之前没有这句显示出来的窗口还是上次的,只是用hide()被隐藏起来而已。
this.Dispatcher.BeginInvoke(new Action(() => card2.Show ()));
Thread t = new Thread(() =>
{
Thread.Sleep(6000);//次线程休眠1秒
Dispatcher.Invoke(new Action(() =>
{
this.Dispatcher.BeginInvoke(new Action(() => card2.Hide ()));
}));
});
t.Start();
}
catch
{
MessageBox.Show("数据录入错误");
}
}
private void btn_Watch_Click(object sender, RoutedEventArgs e)
{
Card card2 = new Card();
card2.Show();
//this.Dispatcher.BeginInvoke(new Action(() => card.Show()));
}
}
public class SendData
{
//面向对象编程
public static void Send_Data(byte data,SerialPort yourPort )
{
byte[] planData = new byte[1] { data };
yourPort.Write(planData, 0, 1);
}
}
#endregion
}
阅读全文
0 0
- wpf做的连接mysql数据库项目——jingyanzongjie
- 如何做WPF项目的多语言
- java 项目连接mysql 数据库
- wpf的数据库Sql连接DataGrid.
- java项目连接mysql数据库的jdbc遇到的问题
- JavaEE项目JDBC连接MySql数据库提示ClassNotFoundException的解决方案
- C#探秘系列(九)WPF连接Mysql数据库
- wpf连接数据库
- WPF连接SQL数据库
- MYSQL数据库的连接
- MySql数据库的连接
- Mysql数据库的连接
- MySQL数据库的连接
- mysql数据库的连接
- MySQL数据库的连接
- MySQL数据库的连接
- mysql数据库的连接
- 最简单的delphi连接mysql数据库——201404
- 联合国曝光,杀手机器人来了,这可能是你见过最恐怖的视频...
- Delphi indy TIdHTTP TIdHTTPServer Post Get
- 使用Django创建第一个web项目
- 最大流建图习题
- String限定类型的HashMap以及一个String工具类
- wpf做的连接mysql数据库项目——jingyanzongjie
- 【Scikit-Learn 中文文档】十四:多类和多标签算法
- bzoj 4465: [Jsoi2013]游戏中的学问
- 环境搭建-Hadoop集群搭建
- Qt 程序获取程序所在路径、用户目录路径、临时文件夹等特殊路径的方法
- 自定义注解校验器
- scala编程04--数组
- 第十二周 Prim算法的验证
- Nginx浏览器缓存设置