str1 = "insert into 领料单 values ('" & TextBox1.Text & "','" & TextBox2.Text & "'," _
+ "'" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "'," _
+ "'" & cb1 & "','" & TextBox7.Text & "','" & DataGridView2.CurrentRow.Cells(0).Value & "'," _
+ "'" & DataGridView2.CurrentRow.Cells(1).Value & "','" & DataGridView2.CurrentRow.Cells(2).Value & "'," _
+ "'" & DataGridView2.CurrentRow.Cells(3).Value & "','" & DataGridView2.CurrentRow.Cells(4).Value & "'," _
+ "'" & DataGridView2.CurrentRow.Cells(5).Value & "','" & DataGridView2.CurrentRow.Cells(6).Value & "'," _
+ "'" & DataGridView2.CurrentRow.Cells(7).Value & "','" & DataGridView2.CurrentRow.Cells(8).Value & "')"
Dim con As New SqlCommand(str1, tt)
con.ExecuteNonQuery()刚才本来想把界面也发上来的,但是发了好几次都失败就不发了,说一下
界面就是form 里面上面有几个textbox 然后下面是一个datagridview(多行数据)
textbox       datagridview
1              1
               2 
               3
我就是想要向数据库中插入三行数据
textbox       datagridview
1              1
1              2
1              3
写了上面的代码,但是只能插入一行,请高人指点,怎样才能同时插入三行,先谢谢了。

解决方案 »

  1.   

    循环一下就可以了,for……next,do while……loop
      

  2.   

    Dim IntI As Integer,CurrentRow 换成 IntI 进行循环
      

  3.   

    dim i as Integer
    str1 = "select * from 领料单"
    Dim con As New SqlCommand(str1, tt) 
    con.ExecuteNonQuery() 
    For i = 1 To 3
     With con
          .AddNew
          .Fields(0) = trim(text1.text)
          .Fields(1) = datagridview2.rows(i).cells(1).value 
          End With
             Next i
     con.Update
     con.Close
    Set con = Nothing
      

  4.   

    插入数据库的问题倒是解决了,但是现在又发现一个问题
    select 出的数据放到了 datagrid 里面
    产品品号             主件品号             零件编号
    1000773100         1030554300        1654564567
    1000773100         1030554300        4687645464
    1000773100         1030554300        8745454456
    然后我写了这样的程序
    Dim i As Integer
        For i = 0 To Me.DataGridView1.Rows.Count - 2
            My.Forms.Form1.TextBox4.Text = Me.DataGridView1.CurrentRow.Cells(0).Value
            My.Forms.Form1.TextBox5.Text = Me.DataGridView1.CurrentRow.Cells(1).Value
            My.Forms.Form1.DataGridView2.Item(0, i).Value = DataGridView1.CurrentRow.Cells(2).Value
            My.Forms.Form1.DataGridView2.Item(1, i).Value = DataGridView1.CurrentRow.Cells(3).Value
            My.Forms.Form1.DataGridView2.Item(2, i).Value = DataGridView1.CurrentRow.Cells(4).Value
            My.Forms.Form1.DataGridView2.Item(3, i).Value = DataGridView1.CurrentRow.Cells(5).Value
        Next i
    老是会出现一个超出索引的错误,请指点程序哪里有问题,非常的感激。
      

  5.   

    多条Insert语句之间用分号(;)分隔。你可以定义一个String Array(Dim SQLArr() as String),一个String元素一条语句,最后ExecSQL = Join(SQLArr,";"),然后直接执行ExecSQL,一次提交的语句数量最好控制在500条以内。
      

  6.   

    -------------------------------------------------
     For i = 0 To Me.DataGridView1.Rows.Count - 2 
    -------------------------------------------------
    当DataGrisview1的行数等于0或1时就会出错.
      

  7.   

    1、用为循环
    2、用Insert into tb select FieldName ... Union All select  FieldName ...