(求助)往数据库中存数据的函数,求各位大神指导!
本人大菜鸟。。。。写个程序串口接受的数据放入数据库中,数据库已建好写入数据的save data函数好像错了,运行会这样
[local]1[/local]
求各位大神帮忙修改程序,谢谢谢谢谢谢。。。。这是save data函数
public void SaveData()
{
string sql;
string[] str = new string[5];
sql = "select count(*) as cnt from data ";
OleDbCommand cmd1 = new OleDbCommand(sql, con);
int cnt = int.Parse(cmd1.ExecuteScalar().ToString()) + 1;
str[0] = cnt.ToString();
str[1] = Int32.Parse(tmp[5] + tmp[4], System.Globalization.NumberStyles.HexNumber).ToString();
str[2] = Int32.Parse(tmp[8] + tmp[9], System.Globalization.NumberStyles.HexNumber).ToString();
switch (Int32.Parse(tmp[8] + tmp[9], System.Globalization.NumberStyles.HexNumber))
{
//加速度
case 3843: str[3] = Int32.Parse(tmp[10] + tmp[11], System.Globalization.NumberStyles.HexNumber) + ":" + Int32.Parse(tmp[12] + tmp[13], System.Globalization.NumberStyles.HexNumber).ToString() + ":" + Int32.Parse(tmp[14] + tmp[15], System.Globalization.NumberStyles.HexNumber).ToString();
flag[1]++;
//timer2.Enabled = true;
if (flag[1] == 5)
{
flag[1] = 0;
//timer2.Enabled = false;
}
break;
default:
break;
}
str[4] = System.DateTime.Now.Year.ToString() + ":" + System.DateTime.Now.Month.ToString() + ":" + System.DateTime.Now.Day.ToString() + ":" + System.DateTime.Now.Hour.ToString() + ":" + System.DateTime.Now.Minute.ToString() + ":" + System.DateTime.Now.Second.ToString();
sql = "insert into data values" + @"('" + str[0] + "','" + str[1] + "','" + str[2] + "','" + str[3] + "','" + str[4] + "')";
OleDbCommand cmd = new OleDbCommand(sql, con);
cmd.ExecuteNonQuery();
sql = "select nodeid from node where nodeid='" + str[1] + "'";
OleDbCommand cmd5 = new OleDbCommand(sql, con);
OleDbDataReader rd = cmd5.ExecuteReader();
bool j = rd.HasRows;
if (j == false)
{
sql = "insert into node(" + "nodeid" + "," + "status" + ")" + "values('" + str[1] + "','" + "online" + "')";
OleDbCommand cmd2 = new OleDbCommand(sql, con);
cmd2.ExecuteNonQuery();
}
else
{
sql = "update node set status ='online'where nodeid='" + str[1] + "'";
OleDbCommand cmd6 = new OleDbCommand(sql, con);
cmd6.ExecuteNonQuery();
}
DataSet dataSet = new DataSet();
OleDbCommand cmd8 = con.CreateCommand();
= "select sensorid from nsrelation where sensorid='" + Int32.Parse(tmp[8] + tmp[9], System.Globalization.NumberStyles.HexNumber).ToString() + "'";
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd8);
adapter.Fill(dataSet);
if (dataSet.Tables[0].Rows.Count == 0)
{
sql = "insert into nsrelation(" + "nodeid" + "," + "sensorid" + ")" + "values('" + str[1] + "','" + Int32.Parse(tmp[8] + tmp[9], System.Globalization.NumberStyles.HexNumber).ToString() + "')";
OleDbCommand cmd3 = new OleDbCommand(sql, con);
cmd3.ExecuteNonQuery();
}
else
{
sql = "update nsrelation set nodeid ='" + str[1] + "'where sensorid='" + Int32.Parse(tmp[8] + tmp[9], System.Globalization.NumberStyles.HexNumber).ToString() + "'";
OleDbCommand cmd7 = new OleDbCommand(sql, con);
cmd7.ExecuteNonQuery();
}
sql = "update sensor set status ='online'where sensorid='" + str[2] + "'";
OleDbCommand cmd4 = new OleDbCommand(sql, con);
cmd4.ExecuteNonQuery();
}