将数据从datagridview保存到数据库mysql

时间:2017-11-19 19:45:16

标签: mysql vb.net datagridview

您好我在cmd.ExecuteNonQuery()部分收到错误我只想将所有显示的或数据内容从Datagridview保存到我的数据库。

我得到一个错误第1行不匹配或类似的东西。我从我的数据库sql输入数据库值但我失败了。我在代码中输错了什么。?

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

    For Each row As DataGridViewRow In DataGridView1.Rows
        Dim constring As String = "server=localhost;userid=root;password=1234;database=dat"
        Using con As New MySqlConnection(constring)
            Using cmd As New MySqlCommand("INSERT INTO studlogs VALUES(@studtags, @idno, @lastxt, @firstxt, @middletxt, @dob, @log, @age, @timein, @crse)", con)
                cmd.Parameters.AddWithValue("@studtags", row.Cells("StudentTag").Value)
                cmd.Parameters.AddWithValue("@idno", row.Cells("StudentID").Value)
                cmd.Parameters.AddWithValue("@lastxt", row.Cells("LastName").Value)
                cmd.Parameters.AddWithValue("@firstxt", row.Cells("FirstName").Value)
                cmd.Parameters.AddWithValue("@middletxt", row.Cells("MiddleName").Value)
                cmd.Parameters.AddWithValue("@dob", row.Cells("Age").Value)
                cmd.Parameters.AddWithValue("@log", row.Cells("Status").Value)
                cmd.Parameters.AddWithValue("@age", row.Cells("Birthday").Value)
                cmd.Parameters.AddWithValue("@timein", row.Cells("Timein").Value)
                cmd.Parameters.AddWithValue("@crse", row.Cells("CourseSec").Value)


                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()
            End Using
        End Using
    Next
    MessageBox.Show("Records inserted.")

End Sub


Private Sub Students_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Try
        DataGridView1.AllowUserToAddRows = False ' Disabled or hide (*) Symbol...

        DataGridView1.RowHeadersVisible = False 'To hide Left indicator..
        DataGridView1.DefaultCellStyle.SelectionBackColor = Color.SteelBlue  'Selection backcolor....
        DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGoldenrodYellow 'Alternating Backcolor.
        DataGridView1.AllowUserToResizeRows = False 'Disabled  row resize...
        DataGridView1.ReadOnly = True
        DataGridView1.MultiSelect = False
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridView1.ShowRowErrors = False
        DataGridView1.ShowCellErrors = False

        table2.Columns.Add("StudentTag", Type.GetType("System.String"))
        table2.Columns.Add("StudentID", Type.GetType("System.Int32"))
        table2.Columns.Add("LastName", Type.GetType("System.String"))
        table2.Columns.Add("FirstName", Type.GetType("System.String"))
        table2.Columns.Add("MiddleName", Type.GetType("System.String"))
        table2.Columns.Add("Age", Type.GetType("System.Int32"))
        table2.Columns.Add("Status", Type.GetType("System.String"))
        table2.Columns.Add("Birthday", Type.GetType("System.String"))
        table2.Columns.Add("Timein", Type.GetType("System.String"))
        table2.Columns.Add("CourseSec", Type.GetType("System.String"))

        If table2.Rows.Count > 0 Then
            logins.Text = table2.Rows.Count.ToString()
        End If
        DataGridView1.DataSource = table2
    Catch ex As Exception

    End Try

0 个答案:

没有答案