保存多条记录

时间:2018-04-20 09:54:04

标签: .net vb.net

在我的Form1中我有3个TextBox。我希望当我点击button1时,将使用ID AutoNumber..Row1(TextBox1)一次注册3个TextBoxes .. Row2(TextBox2).. Row3(TextBox3)。

我的代码是:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
   Try
            con.Open()
            Dim sqsq As String = "INSERT INTO MOUVEMENT(Row1,Row2,Row3)values('" & TextBox1.Text & "','" + TextBox2.Text & "','" + TextBox3.Text & "')"
            Dim command As New SqlCommand(sqsq, con)
            command.ExecuteNonQuery()
            MsgBox("SAVE SUCCES", MsgBoxStyle.MsgBoxRtlReading, "SAVE")
        Catch ex As Exception
            MsgBox(ex.Message)
            MsgBox("SAVE ERROR", MsgBoxStyle.MsgBoxRtlReading, "ERROR")
        Finally
            con.Close()
        End Try
    End Sub

1 个答案:

答案 0 :(得分:0)

只需将您的值放入数组中即可。然后,使用参数循环执行带有每个值的命令的数组

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim arr As String() = {TextBox1.Text, TextBox2.Text, TextBox3.Text}
            Dim con As New SqlConnection("Your connection string")
            Try
                Dim sqsq As String = "INSERT MOUVEMENT (Colume Name) Values (@Text);"
                Dim command As New SqlCommand(sqsq, con)
                command.Parameters.Add("@Text", SqlDbType.VarChar)
                con.Open()
                For Each s As String In arr
                    command.Parameters("@Text").Value = s
                    command.ExecuteNonQuery()
                Next
                con.Close()
                MsgBox("SAVE SUCCES", MsgBoxStyle.MsgBoxRtlReading, "SAVE")
            Catch ex As Exception
                MsgBox(ex.Message)
                MsgBox("SAVE ERROR", MsgBoxStyle.MsgBoxRtlReading, "ERROR")
            Finally
                con.Close()
            End Try
End Sub