我在编码c
时遇到问题,我需要通过使用Visual Studio在Arduino上读取序列来更新数据库中心跳的值,但是当我更新该值时,它将更新该值。数据库。
所以我的问题是我想如何通过电子邮件更新值?我不知道查询是通过电子邮件更新的。
我认为我需要更改此查询的代码:
database.updateQuery = $"UPDATE hbs SET hbvalue = '{heartBeat}'";
这是我的完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Threading;
using MySql.Data.MySqlClient;
namespace ReadSerial
{
class Program
{
static SerialPort _serialPort;
static void Main(string[] args)
{
//Serial port configuration
_serialPort = new SerialPort();
_serialPort.PortName = "COM3";//Set your board COM
_serialPort.BaudRate = 9600;
_serialPort.Open();
while (true)
{
string heartBeat = _serialPort.ReadExisting(); //Read serial input
Console.WriteLine($"Serial Read: {heartBeat}"); //print to console
Thread.Sleep(1000); //delay 1 second
//update database
DatabaseObject database = new DatabaseObject();
database.updateQuery = $"UPDATE hbs SET hbvalue = '{heartBeat}'";
database.UpdateDatabase();
Console.WriteLine();
}
}
}
public class DatabaseObject
{
public string updateQuery = "";
private MySql.Data.MySqlClient.MySqlConnection connection;
private string server;
private string database;
private string user;
private string password;
private string port;
private string connectionString;
private string sslM;
public void UpdateDatabase()
{
//Configure database connection
server = "localhost"; //server IP address
database = "heartbeat"; //database name
user = "root"; //username
password = ""; //password
port = "3306"; //SQL port
sslM = "none"; //SSL requirement
connectionString = String.Format("datasource={0};port={1};username={2};password={3};database={4};SSL Mode={5}", server, port, user, password, database, sslM);
connection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
MySqlCommand command = new MySqlCommand(updateQuery, connection);
command.CommandTimeout = 5;
try
{
connection.Open();
if (command.ExecuteNonQuery() == 1)
{
Console.WriteLine("DATA UPDATED");
}
else
{
Console.WriteLine("Data NOT UPDATED");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
connection.Close();
}
}
}