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
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
C#之设置DataGridView不能够选择多行,只能选择一行...
//一行代码就可以啦... this.dataGridView.MultiSelect = false;
DataGridView 单击选中一整行,只能单选,不能选择多行,只能选择一行
this.GridView1.SelectedIndex = -1; 设置DataGridView的属性SelectionMode为FullRowSelect 这样就使DataGridView不是选择一个字段,而是选择一整行了 设置DataGridView的属性MultiSelect为false 这样就使DataGridView不能够选择多行,只能选择一行了想得到某列的值是要判断DataGr
C#的DataGridView在某一行附近插入一行。
有时候,要 DataGridView里插入一行。不是在最后插入,而是在中间的某一行的前或后插入。 这里要使用到DataGridView里的Insert。 例如:在第一行前插入。 '0是第一行的index DataGridView1.Rows.Insert(0, New DataGridViewRow()); 在第一行的后面插入。 '0是第一行的index DataGridVi
HTML学习---------1.14 在文本中插入一行或多行代码使用的标签<code> <pre>
在介绍语言技术的网站中,避免不了在网页中显示一些计算机专业的编程代码,当代码为一行时,用标签,当代码 语法: 插入一行代码: 代码语言 插入多行代码: 代码语言 标签的主要作用:预格式化的文本.被包围在pre元素中的文本通常会保留空格和换行符; 注意: 标签不只是为显示计算机的源代码时用的,在你需要在网页中预显示格式时都可以使用它,只是标签的一个常见应用就是
SQL中用insert into插入一行或多行记录
INSERT INTO插入一行记录:         INSERT INTO tablename values(v1,v2,...);  --这里需包括所有列的值    若只向指定列插入值:         INSERT INTO tablename (column1,column3) values(v1,v3); INSERT INTO插入多行记录:      若数据来自另一张表
c# winform给dataGridView添加值,插入值,填值,添加数据,添加一行,插入一行数据
this.dataGridView1.Columns.Add("第一列名", "第二列名", "第三列名", "第四列名", "第五列名");  this.dataGridView1.Rows.Add("第一个字段值", "第二个字段值", "第三个字段值", "第四个字段值", "第五个字段值");
DataGridView添加行数据并赋值
<br />                   //添加行数据                    <br />                    DataGridViewRow Row = new DataGridViewRow();<br />                    dataGridView1.RowHeadersWidth = 45;<br />                    int index = dataGridView1.Rows.Add(Row);<br /> 
C# 一个DataGridView 行们的拖拽
昨天摸索了下午,看了网上的很多实例代码,发现真心不能用,哪是什么拖拽,明明就是直接的删除嘛,基本的代码写的都一样,所以自己写啦,试着摸索
Winform DataGridView 插入一行数据
C# DataGridView 插入一行数据
C#的DataGridView中,如何选中新添加的行
C#的DataGridView中,如何选中新添加的行 DataGridView中,一行行的添加数据。 但是添加新的行之后,数据多了之后,由于视图没有移动最新行,所以就不能及时看到新添加的行: 所以需要: 当添加一行后,选中当前新添加的行。 【解决过程】 1.参考: C#中datagridview设置当前选择的行 去试试: ? 1 d