我的表单中有2个datagridviews。两个gridview都显示来自数据库本地db的数据。我现在的问题是我试图将gridview中的数据插入服务器数据库,但我只设法插入1个gridview。第二个gridview没有向服务器数据库插入任何内容。
这是我目前的代码:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
GetProcess_A()
GetProcess_B()
InsertProcess_A()
InsertProcess_B()
End Sub
Private Sub GetProcess_A()
Using cmd As New SqlCommand("SELECT TID,TDESC,TPCNAME,MOD_DATE,SYNC_FLG FROM dbo.ZPA_TERMINAL where SYNC_FLG = 0", LocalDB)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Using dt As New DataTable()
sda.Fill(dt)
DataGridView1.DataSource = dt
End Using
End Using
End Using
End Sub
Private Sub InsertProcess_A()
Dim command As New Data.SqlClient.SqlCommand
command.CommandText = "INSERT INTO dbo.ZPAS_TERMINAL (TID, TDESC, TPCNAME, MOD_DATE) VALUES (@TID, @TDESC, @TPCNAME, @MOD_DATE)"
command.Parameters.Add("@TID", SqlDbType.VarChar)
command.Parameters.Add("@TDESC", SqlDbType.VarChar)
command.Parameters.Add("@TPCNAME", SqlDbType.VarChar)
command.Parameters.Add("@MOD_DATE", SqlDbType.VarChar)
ServerDB.Open()
command.Connection = ServerDB
For i As Integer = 0 To DataGridView1.Rows.Count - 1
command.Parameters(0).Value = DataGridView1.Rows(i).Cells(0).Value
command.Parameters(1).Value = DataGridView1.Rows(i).Cells(1).Value
command.Parameters(2).Value = DataGridView1.Rows(i).Cells(2).Value
command.Parameters(3).Value = DataGridView1.Rows(i).Cells(3).Value
command.ExecuteNonQuery()
Next
ServerDB.Close()
End Sub
Private Sub GetProcess_B()
Using cmd As New SqlCommand("SELECT (SELECT TID FROM dbo.ZPA_TERMINAL) AS TID,PGRP,PGRP_NAME,ACTIVE_FLAG,SYNC_FLG FROM SQLIOT.dbo.ZPA_PROCGRP WHERE SYNC_FLG = 0", LocalDB)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Using dt As New DataTable()
sda.Fill(dt)
DataGridView2.DataSource = dt
End Using
End Using
End Using
End Sub
Private Sub InsertProcess_B()
Dim command As New Data.SqlClient.SqlCommand
command.CommandText = "INSERT INTO dbo.ZPAS_PROCGRP (TID, PGRP, PGRP_NAME, ACTIVE_FLAG) VALUES (@TID, @PGRP, @PGRP_NAME, @ACTIVE_FLAG)"
command.Parameters.Add("@TID", SqlDbType.VarChar)
command.Parameters.Add("@PGRP", SqlDbType.VarChar)
command.Parameters.Add("@PGRP_NAME", SqlDbType.VarChar)
command.Parameters.Add("@ACTIVE_FLAG", SqlDbType.VarChar)
ServerDB.Open()
command.Connection = ServerDB
For i As Integer = 0 To DataGridView2.Rows.Count - 1
command.Parameters(0).Value = DataGridView2.Rows(i).Cells(0).Value
command.Parameters(1).Value = DataGridView2.Rows(i).Cells(1).Value
command.Parameters(2).Value = DataGridView2.Rows(i).Cells(2).Value
command.Parameters(3).Value = DataGridView2.Rows(i).Cells(3).Value
command.ExecuteNonQuery()
Next
ServerDB.Close()
End Sub