gpt4 book ai didi

c# - 如何将串行数据流保存到sql数据库

转载 作者:搜寻专家 更新时间:2023-10-30 19:59:17 27 4
gpt4 key购买 nike

我在存储来 self 的 arduino 防风雨罩的数据时遇到问题。我正在获取数据作为数组并将其转换为字符串,但我的问题是存储它,我仔细检查了与 ms sql 的连接并且它工作正常。我认为问题出在时间戳中,但我不知道如何修复它

这是主要代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using weathertest.Serial;
using System.Configuration;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Timers;




namespace weathertest
{

public partial class Form1 : Form
{

serialmanager _spManager;
public Form1()
{
InitializeComponent();

UserInitialization();
_spManager.StartListening();
}

private void UserInitialization()
{
_spManager = new serialmanager();
serialconfig mySerialSettings = _spManager.CurrentSerialSettings;
serialSettingsBindingSource.DataSource = mySerialSettings;


_spManager.NewSerialDataRecieved += new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved);
this.FormClosing += new FormClosingEventHandler(MainForm_FormClosing);
}



private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
_spManager.Dispose();
}
public static String GetTimestamp(DateTime value) {
return value.ToString("dd-MM-yyyy HH:mm:ss");//yyyyMMddHHmmssffff
}
void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)
{
if (this.InvokeRequired)
{
// Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway.
this.BeginInvoke(new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved), new object[] { sender, e });
return;
}

int maxTextLength = 1000; // maximum text length in text box
if (tbData.TextLength > maxTextLength)
tbData.Text = tbData.Text.Remove(0, tbData.TextLength - maxTextLength);

// This application is connected to a GPS sending ASCCI characters, so data is converted to text
string str = e.Data;
tbData.AppendText(str);
tbData.ScrollToCaret();
string strValue = str;
string[] strArray = strValue.Split(' ');

try
{

foreach (object obj in strArray)
{
string constring = "Data Source=BASHKIM;Initial Catalog=Weather;Integrated Security=True";
SqlConnection conn = new SqlConnection(constring);
conn.Open();
String timeStamp = GetTimestamp(DateTime.Now);

label1.Text = (strArray[0]);
label2.Text = (strArray[1]);
label3.Text = (strArray[2]);
label4.Text = (strArray[3]);
label5.Text = (strArray[4]);
label6.Text = (strArray[5]);
label7.Text = (strArray[6]);
label8.Text = (strArray[7]);
label9.Text = (strArray[8]);
label10.Text = (strArray[9]);
label11.Text = (strArray[10]);
label12.Text = (strArray[11]);
label13.Text = (strArray[12]);
label14.Text = (strArray[13]);
label15.Text = (strArray[14]);
label16.Text = timeStamp;




string query1 = @"INSERT INTO daily (winddir,windspeedmph,windgustdir,windspdmph_avg2m,windgustdir_10m,winddir_avg2m,humidity,tempf,rainin,dailyrainin,pressure,batt_lvl,light_lvl,timeStamp) values ("
+ label1.Text + "," + label2.Text + "," + label3.Text + "," + label4.Text + "," + label5.Text + "," + label6.Text + "," + label7.Text + "," + label8.Text + "," + label9.Text + "," + label10.Text + "," + label11.Text + "," + label12.Text + "," + label3.Text + "," + label14.Text + "," + label16.Text + ")";
conn.Close();
}
richD.AppendText(str);
richD.ScrollToCaret();


}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}



}


private void button1_Click(object sender, EventArgs e)
{
_spManager.StartListening();
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'weatherDataSet2.daily' table. You can move, or remove it, as needed.
//this.dailyTableAdapter.Fill(this.weatherDataSet2.daily);
// TODO: This line of code loads data into the 'weatherDataSet.daily' table. You can move, or remove it, as needed.
// this.dailyTableAdapter.Fill(this.weatherDataSet.daily);


}
}
}

这是我的数据库:

Databasefoto

希望有人能帮忙

最佳答案

query1 是一个字符串。它包含 SQL。您忘记执行该字符串中的 SQL。您需要创建一个 SqlCommand并执行它。

using(var cmd = new SqlCommand(query1, conn))
{
cmd.ExecuteNonQuery();
}

顺便说一句:

您应该在数据库中使用数字类型存储数字数据,否则查询您的数据将是一个巨大的 PITA。

您的数据似乎走的路很奇怪。首先将其复制到 UI。然后将它从 UI 复制到数据库。为什么不将数据一次复制到 UI,一次复制到数据库?

您正在 UI 线程上以紧密循环更新您的 UI。这不会更新您的 UI,因为您的 UI 线程正忙于循环。

关于c# - 如何将串行数据流保存到sql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25706141/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com