如何将动态创建的文本框值插入SQL [英] How to Insert the Dynamically Created Textbox values to SQL

查看:67
本文介绍了如何将动态创建的文本框值插入SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有动态创建的文本框值。



Hi,

I have the Dynamically created text boxes values.

Public Class Form1
 
    WithEvents oBtnAdd As Button
    WithEvents oBtnSubst As Button
 

    Private Sub CreateDynamicControls()
        Dim but As Button = Nothing
        Dim txt As TextBox = Nothing
 
        Dim i As Integer
 
        For i = 1 To 10
            but = New Button
            With but
                .Location = New Point(500, 150 + (i * 25))
                .Tag = i.ToString
                .Name = "ButtonSubst" & i
                .Width = 24
                .Text = "-"
                .Parent = Me
                AddHandler but.Click, AddressOf oBtnSubst_Click
            End With
 
            txt = New TextBox
            With txt
                .Location = New Point(525, 150 + (i * 25))
                .Name = "TextBox" & i
                .Tag = i.ToString
                .Width = 45
                .Text = 0
                .Enabled = False
                .Parent = Me
            End With
 
            but = New Button
            With but
                .Location = New Point(571, 150 + (i * 25))
                .Tag = i.ToString
                .Name = "ButtonAdd" & i
                .Width = 24
                .Text = "+"
                .Parent = Me
                AddHandler but.Click, AddressOf oBtnAdd_Click
            End With
 

        Next i
    End Sub
 
    Public Sub oBtnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles oBtnAdd.Click
        Dim btn As Button = sender
        Dim txt As TextBox = CType(Me.Controls("TextBox" & btn.Tag), TextBox)
        Dim count As Integer = Integer.Parse(txt.Text)
        txt.Text = count + 1
    End Sub
 
    Public Sub oBtnSubst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles oBtnSubst.Click
        Dim btn As Button = sender
        Dim txt As TextBox = CType(Me.Controls("TextBox" & btn.Tag), TextBox)
        Dim count As Integer = Integer.Parse(txt.Text)
        txt.Text = count - 1
    End Sub
 
    Public Sub New()
 
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
 
        ' Add any initialization after the InitializeComponent() call.
        CreateDynamicControls()
    End Sub
End Class





我需要将上面的文本框值插入到sql中。 />


i在下面试过,但只保存了一个数据。请帮助



I Need the above textbox values to be inserted to sql.

i tried below but only one data is getting saved. please help

Dim con As New OleDb.OleDbConnection()
        Dim cmd As New OleDb.OleDbCommand()
        Dim i As Integer
        Dim selected As Object
        Dim it As Integer
        it = ListBox1.Items.Count
        Dim itemsCount As Integer = ListBox1.Items.Count
        For i = 0 To itemsCount - 1
            selected = ListBox1.Items.Item(i)
            con.ConnectionString = "Provider=SQLOLEDB.1;Password=$impadmins123;Persist Security Info=True;User ID=simp;Initial Catalog=Employee Information;Data Source=SRAVI6"
            con.Open()
            cmd.Connection = con
            cmd.CommandText = "insert into Agname (AgentName,t1,t2,t3,t4) values('" & Trim(selected) & "','" & Trim(txt.Text) & "','" & Trim(suptxt.Text) & "','" & Trim(abtxt.Text) & "','" & Trim(intxt.Text) & "')"
            cmd.ExecuteNonQuery()
            con.Close()
        Next i





请帮我保存动态创建要插入sql表的文本框(包括标签的所有文本框值)。



Please help me saving the dynamically created textbox( all textbox values including the label ) to be inserted in sql table.

推荐答案

impadmins123;持久安全信息= True;用户ID = simp;初始目录=员工信息;数据源= SRAVI6
con.Open()
cmd.Connection = con
cmd.CommandText = 插入Agname(AgentName,t1,t2,t3,t4)值('& Trim(selected)& ','& Trim(txt.Text)& ','& Trim(suptxt.Text)& < span class =code-string>','&修剪(abtxt.Text)& ','&修剪(intxt.Text)& ')
cmd.ExecuteNonQuery()
con.Close()
下一步
impadmins123;Persist Security Info=True;User ID=simp;Initial Catalog=Employee Information;Data Source=SRAVI6" con.Open() cmd.Connection = con cmd.CommandText = "insert into Agname (AgentName,t1,t2,t3,t4) values('" & Trim(selected) & "','" & Trim(txt.Text) & "','" & Trim(suptxt.Text) & "','" & Trim(abtxt.Text) & "','" & Trim(intxt.Text) & "')" cmd.ExecuteNonQuery() con.Close() Next i





请帮我保存动态创建要插入sql表格的文本框(包括标签的所有文本框值)。



Please help me saving the dynamically created textbox( all textbox values including the label ) to be inserted in sql table.


试试这个:



Try this:

selected = listBox1.Items(i).ToString


这篇关于如何将动态创建的文本框值插入SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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