如何在Db中插入数据形式的Dinamically文本框行? [英] How Do I Insert Data Form Dinamically Line Of Textboxes In Db?

查看:70
本文介绍了如何在Db中插入数据形式的Dinamically文本框行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试将我的4行生成的文本框中的数据插入到我的sql server 2008表中,我想这样做直到它达到最多5行。

你能不能请帮帮我?

在这里你可以看到我的代码:

Hi i'm trying to insert the data from my line of 4 generated textboxes into my sql server 2008 table and i want to do this until it reaches a max of 5 lines.
Could you help me please?
Here you can see my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Textgenerator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        int c = 1;
        private void buttongenerate_Click_1(object sender, EventArgs e)
        {
           
           
            if (c <= 5)
            {
                TextBox txtRun = new TextBox();
                txtRun.Font = Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                txtRun.Name = "txtDynamic" + (c * 4);
                txtRun.Location = new System.Drawing.Point(163, 143 + (c * 40));
                txtRun.Size = new System.Drawing.Size(80, 23);

                this.Controls.Add(txtRun);
                TextBox txtRun2 = new TextBox();
                txtRun2.Font = Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                txtRun2.Name = "txtDynamic" + (c * 4);
                txtRun2.Location = new System.Drawing.Point(304, 143 + (c * 40));
                txtRun2.Size = new System.Drawing.Size(80, 23);

                this.Controls.Add(txtRun2);
                TextBox txtRun3 = new TextBox();
                txtRun3.Font = Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                txtRun3.Name = "txtDynamic" + (c * 4);
                txtRun3.Location = new System.Drawing.Point(457, 143 + (c * 40));
                txtRun3.Size = new System.Drawing.Size(80, 23);

                this.Controls.Add(txtRun3);
                TextBox txtRun4 = new TextBox();
                txtRun4.Font = Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                txtRun4.Name = "txtDynamic" + (c * 4);
                txtRun4.Location = new System.Drawing.Point(567, 143 + (c * 40));
                txtRun4.Size = new System.Drawing.Size(80, 23);

                this.Controls.Add(txtRun4);

                c++;

            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=.\\SQLSERVER;user id=sa;password=admin5;database=test");
            SqlCommand cmd = new SqlCommand();
            con.Open();
            try
            {
                foreach (Control ctrl in Controls)
                {
                   //if (ctrl.Name != "TextboxInput" )
                   // {
                        if (con != null)
                        {
                            using (con)
                            {
                                int a =5;
                             
                                 for (int i = 1; i <= a; i++)
                                  {
                                    cmd = new SqlCommand("insert into DynamicDBdemo values ('" + ctrl.Text + "')", con);
                                    cmd.CommandType = System.Data.CommandType.Text;
                                    if (con.State == System.Data.ConnectionState.Closed)
                                    {
                                        MessageBox.Show("connection is open");
                                    }
                                }
                                if (con.State == ConnectionState.Open)
                                {
                                    cmd.ExecuteNonQuery();
                                    MessageBox.Show("data added successfully");
                                }
                            }
                        }
                    //}
                }
            }
            catch (SqlException sx)
            {
                MessageBox.Show(sx.Message);
            }
           
                con.Close();
          
        } 
    }
}

推荐答案

你可以找到你的文本框添加如下



you can find the text boxes you added as below

List<TextBox> dynamictextBoxes = this.Controls.OfType<TextBox>().Where(r => r.Name.StartsWith("txtDynamic")).ToList();
foreach (TextBox  txtbox in dynamictextBoxes)
{
   // do something with txtbox 
}


这篇关于如何在Db中插入数据形式的Dinamically文本框行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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