本帖最后由 dongggua 于 2011-09-02 19:52:21 编辑

解决方案 »

  1.   

    建好数据库表
    接收事件内将数组形式的接收数据转换成字符串,然后insert 就可以了哦
      

  2.   

      int n = comm.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致
                byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
                received_count += n;//增加接收计数
                comm.Read(buf, 0, n);//读取缓冲数据
                builder.Remove(0, builder.Length);//清除字符串构造器的内容
                //因为要访问ui资源,所以需要使用invoke方式同步ui。
                this.Invoke((EventHandler)(delegate
                {
                    //判断是否是显示为16禁止
                    if (checkBoxHexView.Checked)
                    {
                        //依次的拼接出16进制字符串
                        foreach (byte b in buf)
                        {
                            builder.Append(b.ToString("X2") + " ");
                        }
                    }
                    else
                    {
                        //直接按ASCII规则转换成字符串
                        builder.Append(Encoding.ASCII.GetString(buf));
                    }
                    //追加的形式添加到文本框末端,并滚动到最后。
                    this.txGet.AppendText(builder.ToString());
                    //修改接收计数
                    labelGetCount.Text = "Get:" + received_count.ToString();
                    DataSet ds = new DataSet();
                    string strSqls;
                    strSqls = string.Format(@"insert into aisxx(信息) values('{0}')", labelGetCount.Text.Trim());
                    SqlConnection conn = new SqlConnection("server = localhost;Initial Catalog =yqjglk;Integrated Security = True ");
                    SqlCommand c3 = new SqlCommand(strSqls, conn);
                    try
                    {
                        conn.Open();
                        c3.ExecuteNonQuery();
                    }
                    catch { }
                    finally
                    {
                        conn.Close();
                        conn.Dispose();
                        c3.Dispose();
                    }
    w我是这么加进去的可是失败