从自动生成的文本框中插入数据到 SQL Server 数据库 [英] Insert data from auto generated textbox to SQL Server database

查看:41
本文介绍了从自动生成的文本框中插入数据到 SQL Server 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a code for generating a text box in vb.net using button click and function

 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 函数中声明.

since txt is declared only in AddNewTextBox function.

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

I have made 3 auto generated text boxs

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

How do I save this data inside the text box to the database?

推荐答案

向表单添加 FlowlayoutPanel 并将 FlowDirection 设置为 TopDown.(如@jmcilhinney 所评论)这节省了计算文本框的位置.

Add a FlowlayoutPanel to your form and set the FlowDirection to TopDown. (as commented by @jmcilhinney) This saves calculating the position of the text boxes.

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

It doesn't make sense to have a function returning a text box when you never use the return value.

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

The data access code uses the .Add method suggested by @SMor. See http://www.dbdelta.com/addwithvalue-is-evil/ and https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ and another one: https://dba.stackexchange.com/questions/195937/addwithvalue-performance-and-plan-cache-implications

我不得不猜测数据类型.检查您的数据库是否有正确的类型.

I had to guess at the datatypes. Check your database for correct types.

值来自添加控件的 FlowLayoutPanel 的控件集合.

The values come from the controls collection of the FlowLayoutPanel where the controls were added.

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

Using blocks ensure that you database objects are closed and disposed even if there is an error. Pass the connection string directly to the constructor of the connection and the command text and connection directly to the constructor of the command.

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

这篇关于从自动生成的文本框中插入数据到 SQL Server 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆