从多个文本框创建数据库表 [英] Create Database tables from multiple textboxes

查看:69
本文介绍了从多个文本框创建数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天。

我目前正在开展一个小型项目,我想通过Windows窗体应用程序创建一个数据库。

我设法创建了从应用程序可以很好地处理数据库,当我需要创建表时,我的问题就出现了。

在应用程序中,我动态地为表名和列名创建文本框(在链接标签点击事件上)。这部分工作正常,我创建每个文本框都有一个唯一的名称,试图创建一个更清晰的时候用它们的列创建表格。

当我需要时我的问题出现了选择我需要创建的表的所有列文本框。

我找到了一些代码(如在create_click中),我试图看看是否可以这样做,但它最终只能识别最后一个表格文本框已添加,而不是其他任何一个。

现在我的问题是。有没有办法让我能够做到这一点,还是我需要完全重新考虑我对此的态度?

我知道表插入代码还没有完成,这不是我的问题,我只是想让列文本框中的文本链接到每个Table文本框下。

很难准确解释我需要做的事情,但是如果你需要更多的澄清请问。

我的代码到目前为止我所做的是。(这是整个表格代码,所以你可以复制和复制应用程序进行测试)



我提前感谢你,并为长篇文章感到抱歉。



Good day all.
I am currently working on a little side project where I want to create a database via a windows forms application.
I have managed to create the database fine from the application, my problem comes in when I need to create the tables.
On the application I dynamically create text boxes (on link label click events) for the table name and column names. This part works fine, I create each text box with a unique name in an attempt to create a bit more clarity when it comes to creating the tables with their columns.
My problem comes in when I need to select all the column text boxes for the table i need to create.
I have found some code(as in the create_click) that I tried to see if i could do this, but it ends up only recognizing the last table text box added and not any of the others.
Now my question is. Is there any way for me to be able to do this or do I need to totally rethink my approach to this?
I am aware that the table insert code is not done yet, that is not my problem, I just simply want the text from the Column text boxes to be linked under each Table text box.
Its hard to explain exactly what I need to do I know, but if you need any more clarification on this please ask.
My code as to what I have done so far is below.(This is the entire form code so you can copy and reproduce the application for testing)

I thank you in advance and sorry for the long post.

private void AddColumn_Click(object sender, EventArgs e)
       {
           if (i == 0)
           {
               i = 1;
           }
           //Label
           lblColumns = new Label();
           Column = new TextBox();
           Types = new TextBox();
           nulls = new CheckBox();
           notnulls = new CheckBox();
           lblColumns.Text = "Column " + i;
           lblColumns.Location = new Point(345, Table.Bottom + (i * 25));
           lblColumns.AutoSize = true;
           //TextBox
           //Column = new TextBox();
           Column.Location = new Point(lblColumns.Location.X + 60, lblColumns.Location.Y);
           Column.Size = Columns.Size;
           Column.Name = Table.Name + Column.Name + "Coulmn" + i;

           //Combobox
           Types.Location = new Point(Type.Location.X, Column.Location.Y);
           Types.Size = Type.Size;
           Types.Name = "Type" + i;

           //Radio Buttons
           nulls.Text = "NULL";
           nulls.Location = new Point(Null.Location.X, Column.Location.Y);
           nulls.AutoSize = true;
           nulls.Checked = false;

           nulls.Name = "nulls" + i;

           notnulls.Text = "NOT NULL";
           notnulls.Location = new Point(NotNull.Location.X, Column.Location.Y);
           notnulls.AutoSize = true;
           notnulls.Checked = true;

           notnulls.Name = "notnulls" + 1;

           CheckBox IDEN = new CheckBox();
           IDEN.Text = "IDEN";
           IDEN.Name = "IDEN" + i;
           IDEN.Location = new Point(Types.Location.X - 60, Types.Location.Y);
           IDEN.AutoSize = true;
           IDEN.Checked = false;
           this.Controls.Add(IDEN);

           this.Controls.Add(nulls);
           this.Controls.Add(notnulls);
           this.Controls.Add(lblColumns);
           this.Controls.Add(Column);
           this.Controls.Add(Types);

           if (i == 1)
           {
               IDEN.Checked = true;
           }

           i++;

           AddTable.Visible = true;


           AddTable.Location = new Point(Tables.Location.X + 154, Column.Location.Y);
           AddColumn.Location = new Point(Columns.Location.X + (Column.Width / 3), Column.Bottom + 5);
           Create.Visible = true;
           Create.Location = new Point(this.Width / 2 - Create.Width / 2, AddColumn.Bottom + 20);
           this.AutoScrollPosition = new Point(Math.Abs(this.AutoScrollPosition.X), this.VerticalScroll.Maximum);

       }
       private void AddTable_Click(object sender, EventArgs e)
       {

           lblTables = new Label();
           Table = new TextBox();
           Table.Name = Table.Name + "Table" + j.ToString();
           lblTables.Text = "Table " + j;
           Table.Size = Tables.Size;

           if (i == 1)
           {
               lblTables.Location = new Point(10, Columns.Bottom + (j * 25));
               Table.Location = new Point(lblTables.Location.X + 60, lblTables.Location.Y);
           }

           if (i > 1)
           {
               lblTables.Location = new Point(10, Column.Bottom + (j * 25));
               Table.Location = new Point(lblTables.Location.X + 60, Column.Bottom + (j * 25));
               AddColumn.Location = new Point(Columns.Location.X + 154, Table.Location.Y);
           }
           this.Controls.Add(Table);
           this.Controls.Add(lblTables);
           i = 0;
           j++;
           AddColumn.Visible = true;
           AddTable.Visible = false;
           Create.Visible = false;
           this.AutoScrollPosition = new Point(Math.Abs(this.AutoScrollPosition.X), this.VerticalScroll.Maximum);


       }
       private void Create_Click(object sender, EventArgs e)
       {
           foreach (Control ctrl in this.Controls)
           {
               if (ctrl.Name.Contains(Table.Name))
               {
                   MessageBox.Show(ctrl.Name);
               }
           }
       }

推荐答案

如果我理解你的问题,你的问题是您正在引用每次创建新对象时被覆盖的全局变量。没有变量维持对所有所创建对象的引用,例如 List< t>< / t> 等。因此,当您遍历表单上的控件时,它会搜索 Table.Name ,而仅包含创建的最新对象,没有以前的对象。您可能需要提取表单上所有表名的列表,然后循环遍历该列表,您可以遍历控件以查找包含表名的对象。



If I understand your question, your issue is that you are referencing a global variable that is being overwritten each time a new object is created. There is no variable maintaining a reference to all the objects created, like a List<t></t> or something. So when you loop through the controls on the form, it searches for the Table.Name, and Table holds only the latest object created, none of the previous ones. You would probably need to extract a list of all the table names on the form, then looping through that list, you could loop through the controls to find objects that contained the table name.

private void Create_Click(object sender, EventArgs e)
{
     foreach (Control ctrl in this.Controls)
     {
          if (ctrl.Name.Contains(Table.Name)) // <-- references a global variable
          {
               MessageBox.Show(ctrl.Name);
          }
     }
}





话虽如此,你可能会更好地重新思考动态构建表和列的过程,包括变量的类型和范围,以便在创建时保持关联。



Having said that, you would probably be better served to rethink the process of dynamically building the tables and columns, including the type and scope of the variables, so that the association would be maintained as the creation occurred.


这篇关于从多个文本框创建数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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