[.NET] 迴圈應用的疑問
我想寫一個迴圈 希望可以每6筆就傳進一次資料庫
傳進資料庫之後把Datagridview的那6筆刪掉 等待新的6筆進來
下面是我自己寫的程式碼 一直出錯 請大大們指教
Dim xrow As DataRow = table.NewRow
Dim intI As Integer = 1
For intI = 1 To 6
xrow.Item(0) = RemoteIpEndPoint.Address.ToString
xrow.Item(1) = txtport.Text
xrow.Item(2) = Encoding.ASCII.GetString(receiveBytes)
table.Rows.Add(xrow)
Next intI
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.121.150.186
推
09/22 13:43, , 1F
09/22 13:43, 1F
→
09/22 13:52, , 2F
09/22 13:52, 2F
推
09/22 16:07, , 3F
09/22 16:07, 3F
→
09/22 16:10, , 4F
09/22 16:10, 4F
→
09/22 16:12, , 5F
09/22 16:12, 5F
推
09/22 16:19, , 6F
09/22 16:19, 6F
→
09/22 16:38, , 7F
09/22 16:38, 7F
→
09/22 16:41, , 8F
09/22 16:41, 8F
推
09/22 16:43, , 9F
09/22 16:43, 9F
Imports System.IO
Imports System.Net.Sockets.Socket
Imports System
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
Public receivingUdpClient As UdpClient
Public RemoteIpEndPoint As New
System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
Public ThreadReceive As System.Threading.Thread
Dim port As Integer
Private table As New DataTable
Public Sub ReceiveMessages()
Dim receiveBytes As [Byte]() =
receivingUdpClient.Receive(RemoteIpEndPoint)
txtip.Text = RemoteIpEndPoint.Address.ToString
Dim BitDet As BitArray
BitDet = New BitArray(receiveBytes)
Dim strReturnData As String =
System.Text.Encoding.Unicode.GetString(receiveBytes)
Dim intI As Integer = 1
For intI = 1 To 6
Dim xrow As DataRow = table.NewRow
xrow.Item(0) = RemoteIpEndPoint.Address.ToString
xrow.Item(1) = txtport.Text
xrow.Item(2) = Encoding.ASCII.GetString(receiveBytes)
table.Rows.Add(xrow)
Next intI
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
port = txtport.Text
receivingUdpClient = New System.Net.Sockets.UdpClient(port)
ThreadReceive = New System.Threading.Thread(AddressOf ReceiveMessages)
ThreadReceive.Start()
txtport.Enabled = True
Button2.Enabled = True
Button1.Enabled = False
DataGridView1.ReadOnly = True
txtport.ReadOnly = True
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
ThreadReceive.Abort()
receivingUdpClient.Close()
Button2.Enabled = False
Button1.Enabled = True
txtip.Text = ""
DataGridView1.Enabled = False
txtport.ReadOnly = False
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
table.Columns.Add("IP")
table.Columns.Add("PORT")
table.Columns.Add("MSG")
DataGridView1.DataSource = table
DataGridView1.Columns(0).Width = 100
DataGridView1.Columns(1).Width = 70
DataGridView1.Columns(2).Width = 230
End Sub
End Class
※ 編輯: mdjason 來自: 140.121.150.186 (09/22 16:45)
→
09/22 16:45, , 10F
09/22 16:45, 10F
推
09/22 16:52, , 11F
09/22 16:52, 11F
→
09/22 16:53, , 12F
09/22 16:53, 12F
→
09/22 16:55, , 13F
09/22 16:55, 13F
Public Sub ReceiveMessages()
Dim receiveBytes As [Byte]() =
receivingUdpClient.Receive(RemoteIpEndPoint)
txtip.Text = RemoteIpEndPoint.Address.ToString
Dim BitDet As BitArray
BitDet = New BitArray(receiveBytes)
Dim strReturnData As String =
System.Text.Encoding.Unicode.GetString(receiveBytes)
***Dim table As New DataTable***
Dim intI As Integer = 1
For intI = 1 To 6
Dim xrow As DataRow = table.NewRow
xrow.Item(0) = RemoteIpEndPoint.Address.ToString
xrow.Item(1) = txtport.Text
xrow.Item(2) = Encoding.ASCII.GetString(receiveBytes)
table.Rows.Add(xrow)
Next intI
End Sub
我改成*** ***那樣 可是還是一次寫入6筆一樣的數據耶
※ 編輯: mdjason 來自: 140.121.150.186 (09/22 16:58)
推
09/22 16:57, , 14F
09/22 16:57, 14F
推
09/22 17:15, , 15F
09/22 17:15, 15F
→
09/22 17:21, , 16F
09/22 17:21, 16F
推
09/22 17:27, , 17F
09/22 17:27, 17F
→
09/22 17:33, , 18F
09/22 17:33, 18F
→
09/22 19:06, , 19F
09/22 19:06, 19F
→
09/22 19:07, , 20F
09/22 19:07, 20F
→
09/22 19:07, , 21F
09/22 19:07, 21F
→
09/22 20:00, , 22F
09/22 20:00, 22F