好吧,我有一堆.txt文件,我用vb.net导入DataGridView
,这些文件总是有不同的列,就像这两个例子一样:
我正在使用下一个代码循环遍历DataGridView的行和列,并且代码也将数据插入到mysql表中,现在,我制作的流程,它是下一个:
这是我的代码:
Private Sub ExportarToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExportarToolStripMenuItem.Click Dim conn As MySqlConnection = New MySqlConnection("Server=localhost;user=root;password=1234;database=chafa;port=3306") conn.Open() Dim comm As MySqlCommand = New MySqlCommand() comm.Connection = conn Dim col1, col2, col3, col4, col5, col6, col7, col8, col9, col10 Dim tabla As New DataTable For i = 2 To DgvDatos.Rows.Add - 1 Step 1 For j = 0 To Me.DgvDatos.Columns.Count - 1 col1 = DgvDatos.Rows(i).Cells(j).Value() col2 = DgvDatos.Rows(i).Cells(j).Value() col3 = DgvDatos.Rows(i).Cells(j).Value() col4 = DgvDatos.Rows(i).Cells(j).Value() col5 = DgvDatos.Rows(i).Cells(j).Value() col6 = DgvDatos.Rows(i).Cells(j).Value() col7 = DgvDatos.Rows(i).Cells(j).Value() col8 = DgvDatos.Rows(i).Cells(j).Value() col9 = DgvDatos.Rows(i).Cells(j).Value() col10 = DgvDatos.Rows(i).Cells(j).Value() comm.CommandText = "insert into ejemplo(col1,col2,col3,col4,col5,col6,col7,col8,col9,col10) values('" & col1 &_ "','" & col2 & "','" & col3 & "','" & col4 & "','" & col5 & "','" & col6 & "','" & col7 & "','" & col8 & "','" & col9 & "','" & col10 & "')" comm.ExecuteNonQuery() Next Next MessageBox.Show("Datos Agregados Correctamente") conn.Close() End Sub
我做错了什么?提前致谢并对不起如果我问了一个重复的问题。
答案 0 :(得分:0)
我使用sql而不是mysql做了类似的事情,但也许它可以帮助你:
query = String.Empty
query &= "UPDATE schedule SET Task = @Task, Complete = @Complete, Start_date = @Start_date, "
query &= "Due_date = @Due_date, JRID = @JRID, Task_Manager = @Task_Manager, Entered_By = @Entered_By, Time_Entered = @Time_Entered "
query &= "WHERE TaskID = @TaskID "
query &= "IF @@ROWCOUNT = 0 INSERT INTO schedule ( TaskID, Task, start_date, Due_Date, Complete, Task_Manager, JRID, Entered_By, Time_Entered)"
query &= " VALUES ( @TaskID, @Task, @start_date, @Due_Date, @Complete, @Task_Manager, @JRID, @Entered_By, @Time_Entered);"
If MainSchedule.isokclicked = 1 Then
For Each row As DataGridViewRow In MainSchedule.DataGridView1.Rows
If Not (row.Cells(0).Value = Nothing) Then
insertcommand.Parameters.Clear()
insertcommand.CommandText = query
insertcommand.Parameters.AddWithValue("@TaskID", row.Cells(0).Value)
insertcommand.Parameters.AddWithValue("@Complete", "False")
insertcommand.Parameters.AddWithValue("@Task", row.Cells(1).Value)
insertcommand.Parameters.AddWithValue("@Start_date", row.Cells(2).Value)
insertcommand.Parameters.AddWithValue("@Due_Date", row.Cells(3).Value)
insertcommand.Parameters.AddWithValue("@JRID", txtJRID.Text)
insertcommand.Parameters.AddWithValue("@Task_Manager", row.Cells(4).Value)
insertcommand.Parameters.AddWithValue("@Entered_By", GetUserName())
insertcommand.Parameters.AddWithValue("@Time_Entered", Now)
insertcommand.ExecuteNonQuery()
End If
keypos = keypos + 1
Next
Connexion.Close()
Else
End If
它只是一个运行参数化查询的基本FOR循环,它可能不是最有效的方法,但它很简单。此代码还将更新数据表,或者如果当前数据库中没有匹配的键,则插入。如果您对我的代码有任何疑问或建议,请不要犹豫,让我知道。