| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 619 人关注过本帖
标题:(求助)往数据库中存数据的函数,求各位大神指导!
取消只看楼主 加入收藏
xlt0703
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-4-13
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
(求助)往数据库中存数据的函数,求各位大神指导!
本人大菜鸟。。。。写个程序串口接受的数据放入数据库中,数据库已建好
写入数据的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();

        }
搜索更多相关主题的帖子: 数据库 数据 public count 
2013-04-13 23:38
xlt0703
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-4-13
收藏
得分:0 
图片貌似显示不了,那我就打出来好了,运行以后messagebox显示exectuescalar 要求可已打开且可用的connections。连接的当前状态为已关闭
2013-04-13 23:43
快速回复:(求助)往数据库中存数据的函数,求各位大神指导!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025019 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved