Home > Net >  Insert a serial port receives the data to the SQL database, receive a data will be inserted into the
Insert a serial port receives the data to the SQL database, receive a data will be inserted into the

Time:11-20

Help BBS everyone a great god, using c # write a serial program, would like to receive the serial port of writing data into the database, can be successfully written to, but there is a problem, receives a data in the database shows two the same record, I am just learning c #, contact programming, don't know where is the problem, also pointed out the mistake please the great god, posted below receives the event source,

Private void serialPort1_DataReceived (object sender, SerialDataReceivedEventArgs e)
{
.//the string readString=serialPort1 ReadExisting ();

//this. Invoke (updateText, new string [] {readString});

Int num=serialPort1. BytesToRead;
Byte [] received_buf=new byte (num);
Receive_count +=num;
SerialPort1. Read (received_buf, 0, num);
Sb. The Clear ();


Foreach (byte b in received_buf)
{
Sb. Append (b.T oString (" X2 ") + ");
}

This. Invoke ((EventHandler) (delegate
{
String time=the Convert. ToString (System. A DateTime. Now);

TextBox2. AppendText (sb. ToString ());
TextBox4. Text="Rx:" + receive_count. ToString () + "Bytes";
TextBox6. Text=time;



String SQLSTR="server=MS - LULZHXBCECCP; User Id=wl; The Pwd=123; The DataBase=chuankou ";
SqlConnection con=new SqlConnection (SQLSTR);
Con. The Open ();

String ckdata=(https://bbs.csdn.net/topics/textBox2.Text.Trim);
String cktime=textBox6. Text. The Trim ();

String sqltianjia="insert into jieshou (shijian, shuju) values ('" + cktime + "', '" + ckdata +") ";
SqlCommand comm=new SqlCommand (sqltianjia, con);
Int I=comm. ExecuteNonQuery ();
If (I==1)
{
TextBox5. Text="add success";
}

The else
{
TextBox5. Text="add failure";
}

Con. The Close ();


}));
  •  Tags:  
  • C#
  • Related