如何采用c#编一个控制台应用程序,接收串口来的数据存入SQL Server数据库,
数据内容如下:Address=0x1Humidity=12Temperature=22SoilMoisture=114Voltage=2798,就是想分别存入相应的列中,...
数据内容如下:Address = 0x1
Humidity = 12
Temperature = 22
SoilMoisture = 114
Voltage = 2798,就是想分别存入相应的列中,列名就是数据的address,humidity等等。计划采用insert语句,我调试的程序如下,但就是存不进去啊!!!
class Program
{
public static string data = null;
static void Main(string[] args)
{
port = new SerialPort("COM1");
port.BaudRate = 19200;
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
if (port.IsOpen)
{
//char ch;
ConsoleKeyInfo i = Console.ReadKey();
while (i.KeyChar != 's')
{
Console.WriteLine("按s键退出, 其它继续读取com口数据.");
}
}
}
static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string ljsjk = "Data Source=.;Initial Catalog=haha;User ID=sa;Password=123";
SqlConnection conn = new SqlConnection(ljsjk);
conn.Open();
byte[] buf = new byte[1024 * 8];
int recvLen = port.Read(buf, 0, buf.Length);
data = Encoding.ASCII.GetString(buf, 0, recvLen);
// Console.WriteLine(DateTime.Now.ToString());
Console.WriteLine(data);
int Index_add = data.IndexOf("Address");//address的起始位置
int id = data[Index_add + 12] - 48;
。。。。。。。。。
//cmd.ExecuteNonQuery();
conn.Close();
}
private static SerialPort port = null;
}
} 展开
Humidity = 12
Temperature = 22
SoilMoisture = 114
Voltage = 2798,就是想分别存入相应的列中,列名就是数据的address,humidity等等。计划采用insert语句,我调试的程序如下,但就是存不进去啊!!!
class Program
{
public static string data = null;
static void Main(string[] args)
{
port = new SerialPort("COM1");
port.BaudRate = 19200;
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
if (port.IsOpen)
{
//char ch;
ConsoleKeyInfo i = Console.ReadKey();
while (i.KeyChar != 's')
{
Console.WriteLine("按s键退出, 其它继续读取com口数据.");
}
}
}
static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string ljsjk = "Data Source=.;Initial Catalog=haha;User ID=sa;Password=123";
SqlConnection conn = new SqlConnection(ljsjk);
conn.Open();
byte[] buf = new byte[1024 * 8];
int recvLen = port.Read(buf, 0, buf.Length);
data = Encoding.ASCII.GetString(buf, 0, recvLen);
// Console.WriteLine(DateTime.Now.ToString());
Console.WriteLine(data);
int Index_add = data.IndexOf("Address");//address的起始位置
int id = data[Index_add + 12] - 48;
。。。。。。。。。
//cmd.ExecuteNonQuery();
conn.Close();
}
private static SerialPort port = null;
}
} 展开
2个回答
展开全部
这个要分2步来处理:
1.是接收com口数据,并整理得到所需要的所有数据: Address,Humidity,Temperature,SoilMoisture,Voltage;
2.数据库操作, 这里是insert数据.
sql语句可以这样写:string strquery = String.Format("insert into table(Address,Humidity,Temperature,SoilMoisture,Voltage) values({0},{1},{2},{3},{4})", Address,Humidity,Temperature,SoilMoisture,Voltage);
执行sql语句.
1.是接收com口数据,并整理得到所需要的所有数据: Address,Humidity,Temperature,SoilMoisture,Voltage;
2.数据库操作, 这里是insert数据.
sql语句可以这样写:string strquery = String.Format("insert into table(Address,Humidity,Temperature,SoilMoisture,Voltage) values({0},{1},{2},{3},{4})", Address,Humidity,Temperature,SoilMoisture,Voltage);
执行sql语句.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询