如何在vb.net中连续读取数据 [英] How to Read Data Continuously in vb.net
本文介绍了如何在vb.net中连续读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我从服务器ip接收数据:192.168.1.254端口:4000连续。
Itz事务日志每个数据包包含54个数据长度。请在下面找到示例事务日志:
<<},!L2014-05- 27,00:40:45,0002,01,0000000000,070229000
我将收到此日志并存储到DB中。我的代码如下:
Hi All,
I am receiving data from server ip: 192.168.1.254 Port: 4000 continuously.
Itz transaction log each packet contains 54 length of data..please find below sample transaction log:
<<},!L2014-05-27,00:40:45,0002,01,0000000000,070229000
I will receive this logs and storing into DB. my code is below:
Private Sub poolmsgfunc(ByVal state As Object)
Try
Dim bufrack(51) As Byte
Dim sa As Integer
Dim tcpClient As New Net.Sockets.TcpClient(ip, port)
Dim ReceivedData As String
Do
networkStream = tcpClient.GetStream()
networkStream.BeginRead(bufrack, 0, bufrack.Length, AddressOf ReadCompletelog, bufrack)
If bufrack(0) = 60 And bufrack(1) = 60 Then
ReceivedData = Encoding.ASCII.GetString(bufrack, 0, 51)
Else
Exit Sub
End If
Loop
Catch
End Try
Catch
End Try
End Sub
Private Sub ReadCompletelog(ByVal AR As IAsyncResult)
Dim item As String = ""
Dim j As Integer
Try
If networkStream.CanRead = False Then
Exit Sub
End If
Dim buffer() As Byte = AR.AsyncState
Dim bytesRead As Integer = networkStream.EndRead(AR)
Dim Message As String = System.Text.Encoding.ASCII.GetString(buffer, 0, bytesRead)
For j = 0 To buffer.Length - 1
item = (item & buffer(j).ToString("X2"))
Next j
'Dim text = txtracklogs.Text.Replace(Chr(60).ToString(), String.Empty)
'txtracklogs.Text = text
If Me.RichTextBox1.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetTextlog)
Me.Invoke(d, New Object() {Message})
' Else
' RichTextBox1.Text = Message & vbCrLf & RichTextBox1.Text
RichTextBox1.Text = Message & vbCrLf
' alarm1.Dispose()
alarm2.Start()
' RichTextBox1.Text = Message
' RichTextBox2.AppendText(Message & vbCrLf)
Pooltransactionmsg()
End If
Catch
End Try
End Sub
Private Sub SetTextlog(ByVal text As String)
Dim totlen As Integer
Dim cs As Byte
Me.RichTextBox1.Text = text
Dim toot As String = text
Dim bufftextstatus As String = toot.Substring(4, 2)
Dim bufftextlog As String = toot.Substring(5, 2)
stringtot = toot.Substring(7, 41)
totlen = Len(stringtot)
Dim checksumtxt As String = toot.Substring(48, 3)
If bufftextlog = "!L" Then
Pooltransactionmsg()
If totlen = 41 Then
insertstring = toot.Substring(7, 41)
dt = insertstring.Substring(0, 10)
tt = insertstring.Substring(11, 8)
userid = insertstring.Substring(20, 4)
slotid = insertstring.Substring(25, 2)
tag = insertstring.Substring(28, 10)
If tag = "0000000000" Then
tag = "-"
End If
msdid = insertstring.Substring(39, 2)
chksum = toot.Substring(48, 3)
'cs = cs Xor msdid
rack = "RACK 1"
live_Transaction_Count = 1
Dim con1 As New SqlConnection(ConfigurationManager.ConnectionStrings("sqlconn").ConnectionString)
Dim sdate As String = Now()
con1.Open()
Dim adapter As SqlDataAdapter = New SqlDataAdapter
Dim datat1 As DataTable = New DataTable
Dim bsource As BindingSource
Dim cd As SqlCommandBuilder = New SqlCommandBuilder(adapter)
adapter = New SqlDataAdapter("Select description from Rack_Messages where Msg_ID = '" & msdid & "'", con1)
datat1 = New DataTable
bsource = New BindingSource
adapter.Fill(datat1) 'Filling
If datat1.Rows.Count > 0 Then
msgdesc = datat1.Rows.Item(0)(0)
End If
con1.Close()
con1.Open()
Dim adapter1 As SqlDataAdapter = New SqlDataAdapter
Dim datat2 As DataTable = New DataTable
Dim bsource1 As BindingSource
Dim cd1 As SqlCommandBuilder = New SqlCommandBuilder(adapter)
Dim usertoint As Integer = userid
adapter1 = New SqlDataAdapter("Select Name from Staff where Staff_ID = '" & usertoint & "'", con1)
datat2 = New DataTable
bsource1 = New BindingSource
adapter1.Fill(datat2) 'Filling
If datat2.Rows.Count > 0 Then
username = datat2.Rows.Item(0)(0)
' staffid = datat2.Rows.Item(0)(1)
End If
Select Case live_Transaction_Count
Case 1
Dim strCommand3 As String = "INSERT INTO Transaction_Logs(Date_Time, Rack, slot,staffid,name,weapon, Description) values('" & dt + " " + tt & "', '" & rack & "', '" & slotid & "', '" & usertoint & "', '" & username & "', '" & tag & "', '" & msgdesc & "' )"
Dim strCommand4 As String = "INSERT INTO Transactionlive_Logs(Date_Time, Rack, slot,staffid,name,weapon, Description) values('" & dt + " " + tt & "', '" & rack & "', '" & slotid & "', '" & usertoint & "', '" & username & "', '" & tag & "', '" & msgdesc & "' )"
Dim command As SqlCommand = New SqlCommand(strCommand3, con1)
Dim command1 As SqlCommand = New SqlCommand(strCommand4, con1)
command.CommandType = CommandType.Text
command.ExecuteNonQuery()
command1.CommandType = CommandType.Text
command1.ExecuteNonQuery()
Pooltransactionmsg()
Translogupdate()
con1.Close()
username = ""
End Select
End If
ElseIf bufftextlog = "!S" Then
End If
End Sub
但是仍然缺少一些交易..请帮我解决这个问题..我正在使用以下线程阅读&更新到数据库
But still am missing few transactions.. please help me out to solve this issue.. am using following thread to read & update into DB
Me.alarm1 = New Threading.Timer(AddressOf poolmsgfunc, Nothing, 1000, updatelog_duration=100)
问候,
Ganesh
Regards,
Ganesh
推荐答案
尝试分离保存和处理数据,您可以保存到快速的文本日志文件,您不会遗漏任何内容并从该文本文件中读取并在后台处理。 />
如果您的Sql server表查询和插入很慢,请尝试删除索引并仅保留所需内容。
Try separating the saving and the processing of your data, you can save to text log file which is fast and you won't miss anything and read from that text file and process in the background.
If your Sql server table queries and inserts are slow try removing indexes and keeping only what is required.
这篇关于如何在vb.net中连续读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文