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

时间:2019-07-10 12:17:28

标签: sql sql-server vb.net textbox

我已经创建了一个代码,用于使用按钮单击和功能在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)

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

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

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

2 个答案:

答案 0 :(得分:1)

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

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

话虽如此,您的AddNewTextBox方法看起来像这样:

public class CustomAuditListener {
    @PrePersist
    public void prePersist(Object obj) {
        MyEntity entity = (MyEntity) obj;
        AuditFields audit = new AuditFields();
        audit.setCreatedBy("CreatedByValueHere")        
        entity.setAuditFields(audit);
    }

    // can add @PreUpdate, etc, here  

}

您的Button的click事件如下:

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

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

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

答案 1 :(得分: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