繁体   English   中英

如何将数据从datagridview插入数据库mysql vb.net

[英]how to insert data from datagridview to database mysql vb.net

这是我的代码

MysqlConn.Open()
            command.Connection = MysqlConn
            command.CommandText = CommandType.Text
            command.CommandText = "INSERT INTO order VALUES (@1,@2,@3,@4,@5,@6,@7,@8)"
            command.Parameters.AddWithValue("@1", rw.Cells(0).Value)
            command.Parameters.AddWithValue("@2", rw.Cells(1).Value)
            command.Parameters.AddWithValue("@3", rw.Cells(2).Value)
            command.Parameters.AddWithValue("@4", rw.Cells(3).Value)
            command.Parameters.AddWithValue("@5", Now())
            command.Parameters.AddWithValue("@6", TextBox6.Text)
            command.Parameters.AddWithValue("@7", TextBox7.Text)
            command.Parameters.AddWithValue("@8", TextBox8.Text)

            command.ExecuteNonQuery()
        Next
        MsgBox("done")
        MysqlConn.Close()

        command.Parameters.Clear()

我已经做了“插入到顺序( names ,.....)值(@ 1,.....),但仍然无法正常工作。即时通讯使用MySQL

       Dim I As Integer
                For I = 0 To Dgrd.Rows.Count - 2
                    Dim row As DataGridViewRow = Dgrd.Rows(I)
                    Dim SQLCON As New SqlConnection(MyDataBaseCon)
                    Dim CMD As SqlCommand
                    Dim S1 As DataGridViewTextBoxCell = row.Cells(1)
                    Dim S2 As DataGridViewTextBoxCell = row.Cells(2)
                    Dim S3 As DataGridViewTextBoxCell = row.Cells(3)
                    CMD = New SqlCommand("Insert Into order 
                   (S1,S2,S3) Values (@S1,@S2,@S3)", SQLCON)
                    SQLCON.Open()
                    CMD.Parameters.Add(New SqlParameter("@S1", SqlDbType.Int)).Value = S1 
                    CMD.Parameters.Add(New SqlParameter("@S2", SqlDbType.Int)).Value = S2
                    CMD.Parameters.Add(New SqlParameter("@S3", SqlDbType.NVarChar, 50)).Value = S3

                   CMD.ExecuteNonQuery()
                   SQLCON.Close()

                Next
                MsgBox("done")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM