将自动生成的文本框中的数据插入 SQL 服务器数据库

Insert data from auto generated textbox to SQL Server database

我创建了一个代码,用于在 vb.net 中使用按钮单击和函数

生成文本框
 Public Function AddNewTextBox() As System.Windows.Forms.TextBox
    Dim txt As New System.Windows.Forms.TextBox()
    Me.Controls.Add(txt)
    txt.Top = cLeft * 30
    txt.Left = 100
    'txt.Text = "TextBox " & Me.cLeft.ToString
    cLeft = cLeft + 1
    txt.ForeColor = Color.DarkGreen
    txt.BackColor = Color.Gray
    txt.Font = New Font("Arial", 14.0, FontStyle.Regular)
    txt.Size = New Size(237, 31)
    txt.Location = New Point(156, 130 + top1)

    Return txt
End Function

在按钮中

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'call the function
        AddNewTextBox()

End Sub

我试过了

cmd.CommandText = "INSERT INTO userlog ([username],[userlastname]) Values ( @username) "
cmd.Parameters.AddWithValue("@username", txt.Text(i).Text)
cmd.Parameters.AddWithValue("@userlastname", txt.Text(i).Text)

但在

中出现错误
txt.Text(i)

因为 txt 仅在 AddNewTextBox 函数中声明。

我制作了3个自动生成的文本框

如何将文本框中的这些数据保存到数据库中?

由于TextBox是添加到Form的控件集合中,所以可以使用生成的TextBox的OfType enumerable method to get all TextBox controls. What's more I would probably assign the Tag到想要的字段名,这样就可以查询控件集合中的第一个实例了标签等于所需字段的文本框。

另外值得一提的是,您可以使用 With 关键字来删除一些不必要的代码。

综上所述,您的 AddNewTextBox 方法将如下所示:

Public Function AddNewTextBox(ByVal fieldName As String) As System.Windows.Forms.TextBox
    Dim txt As New System.Windows.Forms.TextBox()
    Me.Controls.Add(txt)

    With
      .Top = cLeft * 30
      .Left = 100
      '.Text = "TextBox " & Me.cLeft.ToString
      cLeft = cLeft + 1
      .ForeColor = Color.DarkGreen
      .BackColor = Color.Gray
      .Font = New Font("Arial", 14.0, FontStyle.Regular)
      .Size = New Size(237, 31)
      .Location = New Point(156, 130 + top1)
      .Tag = fieldName
    End With

    Return txt
End Function

您的 Button 的点击事件将如下所示:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'call the function
    Dim txt As TextBox = AddNewTextBox("username")
End Sub

您的参数化查询将如下所示:

Dim usernameTextBox As TextBox = Me.Controls.OfType(Of TextBox).FirstOrDefault(Function(txt) txt.Tag IsNot Nothing AndAlso txt.Tag = "username")

If usernameTextBox IsNot Nothing Then
  cmd.Parameters.AddWithValue("@username", usernameTextBox.Text)
End If

将 FlowlayoutPanel 添加到您的表单并将 FlowDirection 设置为 TopDown。 (正如@jmcilhinney 评论的那样)这节省了计算文本框的位置。

当您从不使用 return 值时,使用 return 文本框函数是没有意义的。

数据访问代码使用@SMor建议的.Add方法。看 http://www.dbdelta.com/addwithvalue-is-evil/https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ 还有一个: https://dba.stackexchange.com/questions/195937/addwithvalue-performance-and-plan-cache-implications

我不得不猜测数据类型。检查您的数据库以获取正确的类型。

这些值来自添加控件的 FlowLayoutPanel 的控件集合。

使用块可确保即使出现错误也能关闭和处置数据库对象。将连接字符串直接传递给连接的构造函数,将命令文本和连接直接传递给命令的构造函数。

Public Sub AddNewTextBox()
    Dim txt As New System.Windows.Forms.TextBox()
    txt.Name = "user" & nameTextBox.ToString
    txt.ForeColor = Color.DarkGreen
    txt.BackColor = Color.Gray
    txt.Font = New Font("Arial", 14.0, FontStyle.Regular)
    txt.Size = New Size(120, 30)
    FlowLayoutPanel1.Controls.Add(txt)
End Sub

Private Sub UpdateUsers()
    Using cn As New SqlConnection("Your connection string")
        Using cmd As New SqlCommand("INSERT INTO userlog ([username],[userlastname]) Values ( @username, @userlastname);", cn)
            cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = FlowLayoutPanel1.Controls(0).Text
            cmd.Parameters.AddWithValue("@userlastname", SqlDbType.VarChar).Value = FlowLayoutPanel1.Controls(1).Text
            cn.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Using
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    AddNewTextBox()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    UpdateUsers()
End Sub

编辑

    For Each tb As TextBox In FlowLayoutPanel1.Controls
        If tb.Text = "" Then
            MessageBox.Show("Please fill all text boxes before Updating")
            Return
        End If
    Next