181 lines
6.4 KiB
C#
Raw Normal View History

2025-12-09 09:28:39 +08:00
using MQTT_WinformV1.Service;
using MqttClient;
2025-12-09 10:55:19 +08:00
using MySql.Data.MySqlClient;
2025-12-09 09:28:39 +08:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
namespace MQTT_WinformV1
{
public partial class FormDataZF : Form
{
private System.Timers.Timer _timer;
private DataUploadService _upLoadService;
public DataTable sendDb = new DataTable();
public FormDataZF()
{
InitializeComponent();
_upLoadService = new DataUploadService();
}
private void FormDataZF_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
2025-12-09 10:55:19 +08:00
initDBData();
2025-12-09 09:28:39 +08:00
button2_Click(null,null);
button1_Click(null,null);
}
2025-12-09 10:55:19 +08:00
private void initDBData()
{
this.txtIP.Text = "139.224.232.211";
this.txtPort.Text = "3308";
this.txtUser.Text = "root";
this.txtPwd.Text = "doantech123";
this.txtDBName.Text = "ay2509055-guiyang-fluorescence-lmes";
}
2025-12-09 09:28:39 +08:00
private void _timer_Tick(object sender, ElapsedEventArgs e)
{
2025-12-09 10:55:19 +08:00
2025-12-09 09:28:39 +08:00
DataUpload();
}
//数据上传
private async Task DataUpload()
{
2025-12-09 10:55:19 +08:00
if (TestDbConnection(this.txtIP.Text.Trim(), this.txtPort.Text, this.txtUser.Text, this.txtDBName.Text, this.txtPwd.Text))
{
AppendLog("开始上传数据");
int count = await _upLoadService.QueryDataAsync(this.txtIP.Text.Trim(), this.txtPort.Text, this.txtUser.Text, this.txtDBName.Text, this.txtPwd.Text);
AppendLog(count.ToString() + "条数据已上传!");
}
2025-12-09 09:28:39 +08:00
}
private void AppendLog(string message)
{
try
{
if (InvokeRequired)
{
BeginInvoke(new Action<string>(AppendLog), message);
return;
}
string timeStamped = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}\r\n";
textBoxLog.AppendText(timeStamped);
}
catch (Exception ex)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
int min;
if (!int.TryParse(txtLUploadPL.Text, out min) || min <= 0)
{
min = 30;
}
// 设置定时器(转换为毫秒)
_timer = new System.Timers.Timer(min * 60 * 1000);
_timer.Elapsed += _timer_Tick;
_timer.AutoReset = true; // 是否重复
_timer.Enabled = true; // 开始计时
// 方法:立即执行一次
_timer_Tick(null, null);
}
private async void button2_Click(object sender, EventArgs e)
{
2025-12-09 10:55:19 +08:00
//sendDb = await _upLoadService.getSendDB();
//bindingSource1.DataSource = sendDb;
2025-12-09 09:28:39 +08:00
}
2025-12-09 10:55:19 +08:00
// 测试数据库连接的核心方法
private bool TestDbConnection(string ip, string port, string user, string dbName, string pwd)
{
// 1. 基础参数校验
if (string.IsNullOrWhiteSpace(ip) || string.IsNullOrWhiteSpace(port) ||
string.IsNullOrWhiteSpace(user) || string.IsNullOrWhiteSpace(dbName))
{
MessageBox.Show("IP、端口、用户名、数据库名不能为空", "参数错误",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
// 2. 拼接连接字符串MySQL
MySqlConnectionStringBuilder connStrBuilder = new MySqlConnectionStringBuilder();
connStrBuilder.Server = ip;
// 补充:端口解析异常处理(避免非数字端口崩溃)
if (!uint.TryParse(port, out uint portNum))
{
MessageBox.Show("端口格式错误请输入数字如3306", "连接失败",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
connStrBuilder.Port = portNum; // 端口转无符号整数
connStrBuilder.UserID = user;
connStrBuilder.Password = pwd;
connStrBuilder.Database = dbName;
connStrBuilder.CharacterSet = "utf8mb4"; // 字符集(避免中文乱码)
// 3. 尝试连接数据库
using (MySqlConnection conn = new MySqlConnection(connStrBuilder.ConnectionString))
{
try
{
conn.Open(); // 打开连接同步WinForm UI 会短暂阻塞,简单场景够用)
return true;
}
catch (MySqlException ex)
{
// 替换 switch 表达式为传统 switch 语句
string errorMsg = "";
switch (ex.Number)
{
case 0:
errorMsg = "无法连接到数据库服务器请检查IP/端口是否正确";
break;
case 1045:
errorMsg = "用户名或密码错误!";
break;
case 1049:
errorMsg = "指定的数据库不存在!";
break;
default:
errorMsg = "数据库连接失败:" + ex.Message;
break;
}
MessageBox.Show(errorMsg, "连接失败",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show("未知错误:" + ex.Message, "连接失败",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return false;
}
}
private void button2_Click_1(object sender, EventArgs e)
{
if (TestDbConnection(this.txtIP.Text.Trim(), this.txtPort.Text, this.txtUser.Text, this.txtDBName.Text, this.txtPwd.Text))
{
MessageBox.Show("数据库连接成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
2025-12-09 09:28:39 +08:00
}
}