Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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;
            }
        }
Posted

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900