创建具有多个表应用程序的数据库(长篇帖子...你被告知) [英] Create Database with multiple tables application (Long post... You were told)

查看:87
本文介绍了创建具有多个表应用程序的数据库(长篇帖子...你被告知)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天



我正在创建一个表单应用程序来在sql中创建数据库和表。 (这只是我正在做的一个侧面项目)

我为用户想要创建的每个列和表创建动态文本框。

创建表实际上工作正常当只有一个表的列需要创建时。当我想创建多个表时它不起作用。我知道我只是在某处丢失了一些东西。但对于我的生活,我无法弄清楚它是什么。



如代码所示:每次添加列文本框时,i都会递增。每次添加表格文本框时,
j都会递增。
当j递增时
我被设置为0.

每次增加时都会增加附加了Stringbuilder。



如果需要,完整的源代码位于: https://www.dropbox.com/s/rtnqlsukw5kp86h/Create%20Databases%20Source.doc [ ^ ]



提前谢谢



Good day

I am creating a forms application to create a database and tables in sql. (This is just a side project i am doing)
I create dynamic text boxes for each column and table that the user wants to create.
Creating a table actually works fine when it is only one table with its columns that need to be created. It is when I want to create more than one table that it does not work. I know I am just missing something somewhere. but for the life of me I cant figure out what it is.

As in the code: i is incremented for each time a column text box is added.
j is incremented for each time a table text box is added.
when j is incremented i is set to 0.
added is incremented each time the Stringbuilder is appended.

If needed the full source code is located here: https://www.dropbox.com/s/rtnqlsukw5kp86h/Create%20Databases%20Source.doc[^]

Thank you in advance

try
            {
                sqlCon = new SqlConnection("Data Source='" + server + "';Initial Catalog=" + Datbase + ";Integrated Security=SSPI");
                sqlCon.Open();
                build = new StringBuilder();
                build.Append("USE " + Datbase + "\r\n");

                //foreach (Control ctrl in this.Controls)
                //{
                //    if (ctrl.Name != "")
                //    {
                foreach (string cont in contents)
                {
                    foreach (Control ctrl in this.Controls)
                    {
                        if (ctrl.Name != "")
                        {
                            if (ctrl.Name == cont)
                            {
                                strTable = ctrl.Text.Replace(' ', '_');
                                build.Append("IF NOT EXISTS ( SELECT * FROM sysobjects WHERE name = '" + strTable + "' and xtype = 'U')\r\n");
                                build.Append("CREATE TABLE " + strTable + "\r\n");
                                build.Append("(\r\n");
                            }

                            if (ctrl.Name.Contains(cont) && ctrl.Name != cont)
                            {
                                if (ctrl.Name.Contains("Column"))
                                {
                                    strColumn = ctrl.Text.Replace(' ', '_');
                                }
                                if (ctrl.Name.Contains("Type"))
                                {
                                    strType = ctrl.Text.Replace(' ', '_');
                                }
                                if (ctrl.Name.Contains("null"))
                                {

                                    if (ctrl.Text == "NN")
                                    {
                                        strNull = "NOT NULL";
                                    }
                                    if (ctrl.Text == "N")
                                    {
                                        strNull = "NULL";
                                    }
                                }
                                if (ctrl.Name.Contains("IDEN"))
                                {
                                    strIden = "IDENTITY";
                                }

                                if (IDEN.Checked == true && strColumn != null && strType != null && strNull != null && strIden != null && add == 0)
                                {
                                    Primary = strColumn;
                                    if (i == 2)
                                    {
                                        build.Append(strColumn + " " + strType + " " + strNull + " " + strIden + "\r\n");
                                        build.Append("PRIMARY KEY(" + Primary + ")");
                                        build.Append(")\r\n");
                                    }
                                    else
                                    {
                                        build.Append(strColumn + " " + strType + " " + strNull + " " + strIden + ",\r\n");
                                    }
                                    strType = null;
                                    strNull = null;
                                    strIden = null;
                                    add++;
                                }
                                else
                                {
                                    if (strIden == null && strNull != null && !ctrl.Name.Contains("Column1 ") && !ctrl.Name.Contains("Type1 ") && !ctrl.Name.Contains("nulls1 ") && !ctrl.Name.Contains("notnull1 ") && !ctrl.Name.Contains("IDEN1 ") && strType != null)
                                    {
                                        if (add < i)
                                        {
                                            if (add == (i - 2))
                                            {
                                                build.Append(strColumn + " " + strType + " " + strNull + "\r\n");
                                                build.Append("PRIMARY KEY(" + Primary + ")\r\n");
                                                build.Append(")\r\n");
                                            }
                                            else if (add < (i - 2))
                                            {
                                                build.Append(strColumn + " " + strType + " " + strNull + ",\r\n");
                                                strNull = null;

                                            }
                                            add++;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    add = 0;
                }
                MessageBox.Show(build.ToString());
                cmd = new SqlCommand(build.ToString(), sqlCon);
                cmd.ExecuteNonQuery();
                sqlCon.Close();
                add = 0;
                MessageBox.Show(strTable + " Added");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                sqlCon.Close();
                add = 0;
            }
        }

推荐答案

你可能不想听到这个,但我认为你也是这样做的很多时候。



尝试将逻辑划分一点:

1.制定n层设计,你正在使用一种方法中的UI和数据库元素

2.虽然是&&和||构造可能很有用,当你在一个内部开始有两个或三个以上时,可能是重新考虑逻辑的时候了。

3.你试图立即将GUI元素转换为SQL(通过StringBuilder),而不是尝试使用各自的属性和方法创建表和列类。如果你覆盖toString方法以产生相应的SQL,你的生活将变得更加容易。



希望这会有所帮助。
You probably don't want to hear this, but I think your doing too much at once.

Try to divide the logic a little:
1. Conjure up a n-tier design, you're using UI and database elements in one method
2. Although && and || constructs can be useful, when you start having more than 2 or three of them inside one if, it might be time to rethink the logic.
3. You try to translate the GUI elements immediately to SQL (via StringBuilder), instead try to create "table" and "column" classes with respective properties and methods. If you override the toString method to yield the corresponding SQL, your life will become easier.

Hope this helps.


这篇关于创建具有多个表应用程序的数据库(长篇帖子...你被告知)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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