DataGridView写入SQL,哪句错了?
以下代码哪里错误?
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim x As Integer
For x = 0 To DataGridView1.RowCount - 1
Dim comm As New SqlCommand
Dim value1 As String = ""
Dim value2 As String = ""
Dim value3 As String = ""
Dim value4 As String = ""
value1 = Trim(DataGridView1.Rows(x).Cells(0).Value.ToString)
'未将对象引用设置到对象的实例。
value2 = Trim(DataGridView1.Rows(x).Cells(1).Value.ToString)
value3 = Trim(DataGridView1.Rows(x).Cells(2).Value.ToString)
value4 = Trim(DataGridView1.Rows(x).Cells(3).Value.ToString)
comm.Connection = sqlconnection1
'sqlconnection1已定义
comm.CommandType = CommandType.Text
Dim sqlstr As String = ""
sqlstr = "insert into so_sodetails (cinvname,iquantity,iunitprice,imoney) values('" & value1 & "','" & value2 & "','" & value3 & "','" & value4 & "')"
comm.CommandText = sqlstr
Try
comm.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("失败!")
End Try
....