如何插入所有数据而不是仅获取1行

时间:2014-10-02 06:03:28

标签: mysql vb.net

我想从我的数据库的表列中插入所有数据,但是现在我只得到一行/或简而言之,我想让所有数据列不仅仅是1行,我使用这段代码:

     Try
     For i = 0 To dt.Rows.Count - 1
     networkconn.Open()    
     Dim cmd As New OdbcCommand("INSERT INTO networkdatabase.table1(data,result) values('" & dt.Rows(0).Item("data").ToString & "','" & dt.Rows(0).Item("result").ToString() & "')", networkconn)
     cmd.ExecuteNonQuery()
     networkconn.close
     Next
     Catch ex As Exception
     End Try

这是我的表

 +------------+----------+
 |    data    |  result  |
 +------------+----------+
 | 1234567890 | PASSED   |
 | Test       | PASSED   |
 | SAMPLE     | FAILED   |
 | test       | FAILED   |
 | GGGGGG     | PASSED   |
 | 1111111111 | PASSED   |
 | XXXXXXXXXX | PASSED   |
 +------------+----------+

我的输出

 +------------+----------+
 |    data    |  result  |
 +------------+----------+
 | 1234567890 | PASSED   |
 +------------+----------+

预期输出

 +------------+----------+
 |    data    |  result  |
 +------------+----------+
 | 1234567890 | PASSED   |
 | Test       | PASSED   |
 | SAMPLE     | FAILED   |
 | test       | FAILED   |
 | GGGGGG     | PASSED   |
 | 1111111111 | PASSED   |
 | XXXXXXXXXX | PASSED   |
 +------------+----------+

2 个答案:

答案 0 :(得分:0)

您可以使用更多行创建SQL命令。允许在一个字符串中传递多个命令。只需用分号分隔

 INSERT INTO table values(x,y,z); 
 INSERT INTO table values(x,y,z); 
 INSERT INTO table values(x,y,z);

答案 1 :(得分:0)

你必须像这样插入td中的每一行:

        For i = 0 To dt.rows.count - 1
          Dim cmd As New OdbcCommand("INSERT INTO networkdatabase.table1(data,result) values('" & dt.Rows(i).Item("data").ToString & "','" & dt.Rows(i).Item("result").ToString() & "')", ServerCN)
       Next