串口接收到了数据如何直接存入SQLSERVER数据库中呢 本帖最后由 dongggua 于 2011-09-02 19:52:21 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 建好数据库表接收事件内将数组形式的接收数据转换成字符串,然后insert 就可以了哦 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我是这么加进去的可是失败 C#新手问:c#实现图片缩放时遇瓶颈,求大神 windows 界面应用程序界面实现的问题 vb转换为C#,我改了但还是有问题,谁能帮帮我? .net/C#如何获取客户端ip地址 关于 c#应用程序 再次升级的问题. 在Windows服务中的timer 初级问题.. 帮帮我,怎么我用生成的xml文件没有回行,我用的是XmlDocument的Save方法存的 这样获取缓冲区里的密码? 有关c#的编译工具 泛型邦定对象 c# “将截断字符串或二进制数据。”怎么解决??
接收事件内将数组形式的接收数据转换成字符串,然后insert 就可以了哦
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我是这么加进去的可是失败