【问题标题】:Insert data from auto generated textbox to SQL Server database将自动生成的文本框中的数据插入 SQL Server 数据库
【发布时间】:2019-07-10 12:17:28
【问题描述】:

我已经创建了一个使用按钮单击和函数在 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 个自动生成的文本框

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

【问题讨论】:

  • 您需要将控件添加到分配给成员变量的列表中,以便以后可以从任何地方访问它,或者您需要从Controls 您将它们添加到的表单集合。
  • 不要先将控件添加到表单中,然后再设置它的位置和其他属性。创建它,配置它,然后添加它。另外,使用TableLayoutPanelFlowLayoutPanel 自动处理定位。
  • 相关 - 不要使用addwithvalue
  • 感谢大家的支持

标签: sql sql-server vb.net textbox


【解决方案1】:

在表单中添加一个 FlowlayoutPanel 并将 FlowDirection 设置为 TopDown。 (由@jmcilhinney 评论)这可以节省计算文本框的位置。

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

数据访问代码使用@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

【讨论】:

  • 谢谢@Mary,这项工作很棒,所以如果我想在一列中添加多个名称,例如一列中的用户名,cmd 查询应该如何?意思是如果我有一个带有 id 和名称的表,如何将动态文本框中的数据添加到相同的“名称”列?像这个例子linklink
  • 我已经在这个代码link 和这个表单组件link 中实现了它,并且在执行 cmd link 之后这是对的吗? @玛丽
  • 和@Mary 如何确定所有生成的文本框都填充了数据?
  • 如果您使用的是 FlowLayoutPanel,则不应使用 .Top、.Left 或 .Location 或任何 cLeft。 FlowLayoutPanel 会为您处理定位。
  • 查看我对答案的编辑以确定文本框是否为空。
【解决方案2】:

由于 TextBox 正在被添加到 Form 的控件集合中,因此您可以使用 OfType 枚举方法来获取所有 TextBox 控件。更重要的是,我可能会将生成的 TextBox 的 Tag 分配给所需的字段名称,以便您可以在控件集合中查询 TextBox 的第一个实例,其标签等于所需的字段。

另外值得一提的是,您可以使用 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

【讨论】:

    猜你喜欢
    • 2014-11-14
    • 2016-05-16
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 2016-09-02
    相关资源
    最近更新 更多