2 allbluekos allbluekos 于 2015.07.04 11:45 提问

Datagridview 不能插入多行,每次只插入一行,请帮忙分析代码问题所在,谢谢。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim mycn As New SqlClient.SqlConnection
    Dim mycom As New SqlClient.SqlCommand
    mycn.ConnectionString = "Data Source=192.168.0.12;Initial Catalog=blue;uid=sa;pwd=123"
    Dim i As Int32
    Dim a1 As String = DataGridView1.Rows(i).Cells(0).Value.ToString
    Dim a2 As String = DataGridView1.Rows(i).Cells(1).Value.ToString

    mycom.CommandText = " insert into knitAM   values('" & a1 & "','" & a2 & "') "
    mycom.Connection = mycn
    Try
        mycn.Open()
        For i = 0 To DataGridView1.Rows.Count
            Dim mysqlreader As Data.SqlClient.SqlDataReader = mycom.ExecuteReader
            mycom.ExecuteNonQuery()
        Next
    Catch ex As Exception
        MsgBox("保存成功")
    Finally
        mycn.Close()
    End Try
End Sub

3个回答

frank_20080215
frank_20080215   2015.07.04 12:37
已采纳

mycom.CommandText = " insert into knitAM values('" & a1 & "','" & a2 & "') "这一句要用循环的。

allbluekos
allbluekos   2015.07.06 08:57

谢谢你,但是我按照你的提示,把这一句放到FOR循环里面,还是不能插入多行哦。我把全部源代码贴上。请帮我看看,谢谢。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ComboBox1.Text = "" Or TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or TextBox5.Text = "" Or TextBox6.Text = "" Then

        MsgBox("Please input above")
    End If

    Dim mycn As New SqlClient.SqlConnection
    Dim mycom As New SqlClient.SqlCommand
    mycn.ConnectionString = "Data Source=192.168.0.2;Initial Catalog=blue;uid=sa;pwd=123"

    Try

        For i = 0 To DataGridView1.Rows.Count
            Dim a1 As String = DataGridView1.Rows(i).Cells(0).Value.ToString
            Dim a2 As String = DataGridView1.Rows(i).Cells(1).Value.ToString
            Dim a3 As String = ""
            Dim a4 As String = DataGridView1.Rows(i).Cells(3).Value.ToString
            Dim a5 As String = DataGridView1.Rows(i).Cells(4).Value.ToString
            Dim a6 As String = DataGridView1.Rows(i).Cells(5).Value.ToString
            If ComboBox1.Text = "夜班" Then
                a3 = DateTimePicker1.Value.AddDays(1).ToString("yyyyMMdd") '11號夜班是10號夜班
            Else
                a3 = DateTimePicker1.Value.ToString("yyyyMMdd")
            End If
            mycom.CommandText = "if exists(select kamID from knitAM  where kamID='" & a6 & "') begin update KnitAM set kamDate =convert(varchar,'" & a3 & "',120),kamShift= '" & a4 & "',kamEID='" & a1 & "' ,kamENM ='" & a2 & "',kamMID='" & a5 & "' where kamID ='" & a6 & "' end else begin insert into knitAM   values('" & a6 & "',convert(varchar,'" & a3 & "',120),'" & a4 & "', '" & a1 & "','" & a2 & "','" & a5 & "') end"
            mycom.Connection = mycn
            mycn.Open()
            Dim mysqlreader As Data.SqlClient.SqlDataReader = mycom.ExecuteReader
            mycom.ExecuteNonQuery()
        Next


    Catch ex As Exception
        MsgBox("保存成功")
    Finally
        mycn.Close()
    End Try
End Sub
allbluekos
allbluekos   2015.07.06 10:16
  修改好了,把TRY放在FOR循环里面。谢谢
        For i = 0 To DataGridView1.Rows.Count - 1
        Dim a1 As String = DataGridView1.Rows(i).Cells(0).Value.ToString
        Dim a2 As String = DataGridView1.Rows(i).Cells(1).Value.ToString
        Dim a3 As String = ""
        Dim a4 As String = DataGridView1.Rows(i).Cells(3).Value.ToString
        Dim a5 As String = DataGridView1.Rows(i).Cells(4).Value.ToString
        Dim a6 As String = DataGridView1.Rows(i).Cells(5).Value.ToString
        If ComboBox1.Text = "夜班" Then
            a3 = DateTimePicker1.Value.AddDays(1).ToString("yyyyMMdd") '11號夜班是10號夜班
        Else
            a3 = DateTimePicker1.Value.ToString("yyyyMMdd")
        End If
        Dim mycom As New SqlClient.SqlCommand
        mycom.CommandText = "'insert into knitAM  "
        mycom.Connection = mycn
        Try
            mycn.Open()
            Dim mysqlreader As Data.SqlClient.SqlDataReader = mycom.ExecuteReader
            mycom.ExecuteNonQuery()

        Catch ex As Exception

        Finally
            mycn.Close()
        End Try
    Next
    MsgBox("更新成功")
Csdn user default icon
上传中...
上传图片
插入图片