dcsimg
Results 1 to 7 of 7

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2018
    Posts
    18

    I need faster code to save update data to ms.sql server 2008r2

    I use Socket for receive data, i split data incoming when still in byte, so i thing it will be realtime when show in my computer. The problem when i try to update data incoming to sql server database, it will make a delay. My question , can you help me to make faster to save/update data to sql server ? thx guys

    my code when receive data from socket
    Code:
        Private Sub doRead(ByVal ar As System.IAsyncResult)
            Dim totalRead As Integer
            Try
                totalRead = client.GetStream.EndRead(ar)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return
            End Try
            Dim start As Integer = 0
            Dim receivedString As String
            If totalRead > 0 Then
                For i = 0 To totalRead - 1
                    If readBuffer(i) = EofLF Then
                            receivedString = Encoding.ASCII.GetString(readBuffer, start, i - start)
                            start = i
                            messageReceived(RestMsg & receivedString)
                            RestMsg = ""
                    End If
                Next
                If readBuffer(totalRead - 1) <> EofLF Then
                    RestMsg = Encoding.ASCII.GetString(readBuffer, start, totalRead - 1 - start)
                End If
    
            End If
    
            Try
                client.GetStream.BeginRead(readBuffer, 0, BYTES_TO_READ, AddressOf doRead, Nothing) 'Begin the reading again.
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub

    my code when received data and using thread and queue
    Code:
       Private Sub messageReceived(ByVal message As String)
            UpdateStatusRTB(Me.RichTextBox1, message)
            Try
                Select Case message
                    Case vbCrLf
                    Case vbLf
                     Case Else
                        Dim MsgSplit() As String
                        MsgSplit = Split(message, "|")
                        If MsgSplit.Length < 10 Then Exit Sub
                        UpdateStatusTB(Me.TextBox1, MsgSplit(2))
                        Select Case MsgSplit(4) ' kode datafeed
                            Case 1
                                sqlstr = "dbo.spUpdateDatafeedOrder '" & MsgSplit(7).Trim & "','" & MsgSplit(8).Trim & "'," & Convert.ToDouble(MsgSplit(15).Trim) & "," & Convert.ToDouble(MsgSplit(16).Trim) & "," & Convert.ToDouble(MsgSplit(17).Trim) & "," & Convert.ToDouble(MsgSplit(18).Trim)
                                OrderQueue.Enqueue(sqlstr)
                                Dim ThreadOrder As New System.Threading.Thread(AddressOf UpdateDataOrder)
                                ThreadOrder.Start()
                            Case 2
                                sqlstr = "dbo.spUpdateDatafeedTrade '" & MsgSplit(7).Trim & "','" & MsgSplit(8).Trim & "'," & Convert.ToDouble(MsgSplit(10).Trim) & "," & Convert.ToDouble(MsgSplit(16).Trim) & "," & Convert.ToDouble(MsgSplit(17).Trim) & "," & Convert.ToDouble(MsgSplit(18).Trim) & "," & Convert.ToDouble(MsgSplit(19).Trim)
                                TradeQueue.Enqueue(sqlstr)
                                Dim ThreadTrade As New System.Threading.Thread(AddressOf UpdateDatatrade)
                                ThreadTrade.Start()
    
                            Case 5
                                sqlstr = "dbo.spUpdateDatafeedSummary '" & MsgSplit(5).Trim & "','" & MsgSplit(6).Trim & "'," & Convert.ToDouble(MsgSplit(7).Trim) & "," & Convert.ToDouble(MsgSplit(8).Trim) & "," & Convert.ToDouble(MsgSplit(9).Trim) & "," & Convert.ToDouble(MsgSplit(18).Trim) & "," & Convert.ToDouble(MsgSplit(19).Trim) & "," & Convert.ToDouble(MsgSplit(20).Trim) & "," & Convert.ToDouble(MsgSplit(21).Trim)
                                SummQueue.Enqueue(sqlstr)
                                Dim ThreadSumm As New System.Threading.Thread(AddressOf UpdateDataSumm)
                                ThreadSumm.Start()
                            Case Else
                                sqlstr = ""
                        End Select
    
    
                End Select
                UpdateStatusRTB(Me.RichTextBox1, "-------------")
            Catch ex As Exception
                UpdateStatusRTB(Me.RichTextBox2, "Err. Split Desc:" & ex.Message & "Err.Msg : " & message)
            End Try
        End Sub
    Code:
        Private Sub UpdateDataOrder()
            Try
                Dim SqlCmd As SqlClient.SqlCommand
                If Not dequeueingOrder Then
                    dequeueingOrder = True
                    While OrderQueue.Count <> 0
                        con.Open()
                        SqlCmd = New SqlCommand(OrderQueue.Dequeue(), con)
                        SqlCmd.ExecuteNonQuery()
                        con.Close()
                    End While
                    dequeueingOrder = False
                End If
            Catch ex As Exception
                UpdateStatusRTB(RichTextBox2, "err.desc Order : " & ex.Message & " err.mesg :" & OrderQueue.Dequeue())
            End Try
    Code:
    UpdateDataSumm , UpdateDatatrade is same like  UpdateDataOrder
    Last edited by esugiawan; Feb 6th, 2018 at 03:55 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width