我做了一个名为form1.vb
的表格。
我有4个名为supid
,supname
,supmobile
和suploc
的文本框。
我有一个名为datagridview1
的DataGridView。
我正在DataGridView中传输这4个TextBoxes数据,但是当我尝试在SQL数据库中发送DataGridView行时,它仅发送1行。
单击插入按钮时,我希望一次在SQL Server中插入多行。
有很多关于C语言的教程,但是没有关于VB.Net
的教程。
请在VB.Net
中帮助如何执行此操作。
我的代码:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Then
MessageBox.Show("PLEASE FILL ALL FIELDS")
Else
connection.Open()
Dim thequery As String = "select*from supplierdata where supmobile = @supmobile"
Dim cmd1 As SqlCommand = New SqlCommand(thequery, connection)
cmd1.Parameters.AddWithValue("@supmobile", TextBox3.Text)
Dim reader As SqlDataReader = cmd1.ExecuteReader()
If reader.HasRows Then
MessageBox.Show("MOBILE NO IS ALREADY REGISTERED")
Else
reader.Close()
command = New SqlCommand("INSERT INTO supplierdata VALUES (@supid,@supname,@supmobile,@suploc)", connection)
Dim i As Integer = 0
For Each row As DataGridViewRow In DataGridView1.Rows
command.Parameters.Add("supid", SqlDbType.Int).Value = DataGridView1.Rows(i).Cells(0).value
command.Parameters.Add("supname", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(1).Value
command.Parameters.Add("supmobile", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(2).Value
command.Parameters.Add("suploc", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(3).Value
i = command.ExecuteNonQuery()
Next
MessageBox.Show("CUSTOMER REGISTERED SUCCESFULLY")
connection.Close()
command.Dispose()
End If
End If
End Sub
最佳答案
第一个If语句中的
OrElse
而不是Or
。一旦检测到True,这将停止评估。 Using...End Using
块将解决这一问题。 DataReader
。只需使用ExecuteScalar
即可。 Dim i As Integer = 0
然后在For Each循环中使用DataGridView1.Rows(i).Cells(0).Value
。行始终是第一行,它在循环中永远不会改变。这就是为什么“仅发送1行” For Each
循环中添加参数。 subid
是自动增量列,则不要为此列发送任何数据。 未经测试的代码。我没有您的表格或数据库。
Private Sub OPCode2()
If TextBox2.Text = "" OrElse TextBox3.Text = "" OrElse TextBox4.Text = "" Then
MessageBox.Show("PLEASE FILL ALL FIELDS")
Return
End If
Dim count As Integer
Try
Using cn As New SqlConnection("Your connection string")
Dim thequery As String = "select Count(*) from supplierdata where supmobile = @supmobile;"
Using cmd1 As SqlCommand = New SqlCommand(thequery, cn)
cmd1.Parameters.AddWithValue("@supmobile", TextBox3.Text)
cn.Open()
count = CInt(cmd1.ExecuteScalar())
End Using
End Using
Catch ex As Exception 'You probably want to catch more specific exceptions
'and handle them separately
MessageBox.Show(ex.Message)
End Try
If count > 0 Then
MessageBox.Show("MOBILE NO IS ALREADY REGISTERED")
Return
End If
Try
Using cn As New SqlConnection("Your connection string")
Using cmd2 As New SqlCommand("INSERT INTO supplierdata (supid,supname,supmobile,suploc)VALUES (@supid,@supname,@supmobile,@suploc)", cn)
cmd2.Parameters.Add("@supid", SqlDbType.Int)
cmd2.Parameters.Add("@supname", SqlDbType.NChar)
cmd2.Parameters.Add("@supmobile", SqlDbType.NChar)
cmd2.Parameters.Add("@suploc", SqlDbType.NChar)
cn.Open()
For Each row As DataGridViewRow In DataGridView1.Rows
With cmd2
.Parameters("@supid").Value = row.Cells(0).Value
.Parameters("@supname").Value = row.Cells(1).Value
.Parameters("@supmobile").Value = row.Cells(2).Value
.Parameters("@suploc").Value = row.Cells(3).Value
End With
cmd2.ExecuteNonQuery()
Next
End Using 'You probably want to catch more specific exceptions
'and handle them separately
End Using
Catch ex As Exception 'You probably want to catch more specific exceptions
'and handle them separately
MessageBox.Show(ex.Message)
End Try
MessageBox.Show("CUSTOMER REGISTERED SUCCESFULLY")
End Sub
编辑
如果
supid
是一个自动增量列,请从参数中删除它。将插入查询更改为
"INSERT INTO supplierdata (supname,supmobile,suploc)VALUES (@supname,@supmobile,@suploc)"
然后您需要删除行cmd2.Parameters.Add("@supid", SqlDbType.Int)
和.Parameters("@supid").Value = row.Cells(0).Value
仅当supid
是并且自动递增(标识列)时,才执行此操作