Beispiel #1
0
        /// <summary>
        /// event is fired when the 'ImportButton' is clicked.
        /// </summary>
        private void CreateSQL_Click(object sender, EventArgs e)
        {
            // set the fileName
            string fileName = SourceFileControl.Text;

            // locals
            TextLine    firstRowContent = null;
            List <Word> words           = null;

            char[]        delimiters          = { ',' };
            StringBuilder sb                  = new StringBuilder();
            string        alterTableTemplate  = "Alter Table RawImport";
            string        alterTableTemplate2 = "Add [ColumnName] nvarchar(255) null";
            string        alterTable          = "";

            // if the fileName exists
            if ((TextHelper.Exists(fileName)) && (File.Exists(fileName)))
            {
                // read the text of the file
                string fileText = File.ReadAllText(fileName);

                // set the textLines
                List <TextLine> textLines = WordParser.GetTextLines(fileText);

                // If the textLines collection exists and has one or more items
                if (ListHelper.HasOneOrMoreItems(textLines))
                {
                    // set the first row contents
                    firstRowContent = textLines[0];
                }

                // If the firstRowContent object exists
                if (NullHelper.Exists(firstRowContent))
                {
                    // parse the words from the first column
                    words = WordParser.GetWords(firstRowContent.Text, delimiters);
                }

                // If the words collection exists and has one or more items
                if (ListHelper.HasOneOrMoreItems(words))
                {
                    // Iterate the collection of Word objects
                    foreach (Word word in words)
                    {
                        // remove any spaces and special characters from the column names. This may have to be modified as more files are tested.
                        word.Text = word.Text.Replace(" ", "").Replace("(", "").Replace(")", "").Replace(".", "").Replace("_", "").Replace("<", "").Replace(">", "");

                        // Start on a new line
                        sb.Append(Environment.NewLine);

                        // add this one
                        sb.Append(alterTableTemplate);

                        // Start on a new line
                        sb.Append(Environment.NewLine);

                        // replace out the text of the template
                        alterTable = alterTableTemplate2.Replace("[ColumnName]", TextHelper.CapitalizeFirstChar(word.Text));

                        // add this one
                        sb.Append(alterTable);

                        // Start on a new line
                        sb.Append(Environment.NewLine);

                        // Append Go
                        sb.Append("Go");
                    }

                    // set the sqlQuery and trim off the last line
                    string sqlQuery = sb.ToString().Trim();

                    // copy to clipboard
                    Clipboard.SetText(sqlQuery);

                    // Show finished message
                    MessageBoxHelper.ShowMessage("Finished", "Done");
                }
            }
        }
        /// <summary>
        /// event is fired when the 'ImportLastNamesButton' is clicked.
        /// </summary>
        private void ImportLastNamesButton_Click(object sender, EventArgs e)
        {
            // locals
            bool        saved    = false;
            string      fileName = "";
            List <Word> words    = null;

            char[] delimiters = { ' ' };
            bool   skipName   = false;

            try
            {
                // get the text of the LastNames.txt file
                string LastNamesTextFile = LastNamesControl.Text;

                // attempt to read all the text
                string textFileContents = File.ReadAllText(LastNamesTextFile);

                // If the textFileContents string exists
                if (TextHelper.Exists(textFileContents))
                {
                    // create the fileInfo
                    FileInfo fileInfo = new FileInfo(LastNamesTextFile);

                    // set the name of the file
                    fileName = fileInfo.Name;

                    // get the text lines (this file is one entry per row)
                    List <TextLine> lines = WordParser.GetTextLines(textFileContents);

                    // If the lines collection exists and has one or more items
                    if (ListHelper.HasOneOrMoreItems(lines))
                    {
                        // Setup Graph2
                        Graph2.Visible = true;
                        Graph2.Maximum = lines.Count;
                        Graph2.Value   = 0;

                        // refresh everything
                        Refresh();

                        // Create a new instance of a 'Gateway' object.
                        Gateway gateway = new Gateway();

                        // Iterate the collection of TextLine objects
                        foreach (TextLine line in lines)
                        {
                            // reset
                            skipName = false;

                            // if this name contains weird symbols
                            if (line.Text.Contains("^"))
                            {
                                // skip this name
                                skipName = true;
                            }
                            else if (line.Text.Contains(" or "))
                            {
                                // skip this name
                                skipName = true;
                            }
                            else if (line.Text.Contains("-"))
                            {
                                // skip any hyphenated names
                                skipName = true;
                            }

                            // if the value for skipName is false
                            if (!skipName)
                            {
                                // Create a LastName instance
                                dataObjects.LastName LastName = new dataObjects.LastName();

                                // get the words
                                words = WordParser.GetWords(line.Text, delimiters);

                                // if there is a space in the name
                                if ((ListHelper.HasOneOrMoreItems(words)) && (words.Count > 1))
                                {
                                    // Create a new instance of a 'StringBuilder' object.
                                    StringBuilder sb = new StringBuilder();

                                    // Iterate the collection of Word objects
                                    foreach (Word word in words)
                                    {
                                        // Capitalize each word
                                        sb.Append(TextHelper.CapitalizeFirstChar(word.Text));
                                        sb.Append(" ");
                                    }

                                    // set the LastName
                                    LastName.Name = sb.ToString().Trim();
                                }
                                else
                                {
                                    // set the LastName
                                    LastName.Name = TextHelper.CapitalizeFirstChar(line.Text.Trim());
                                }

                                // save this LastName
                                saved = gateway.SaveLastName(ref LastName);

                                // if the value for saved is true
                                if (saved)
                                {
                                    // increment the value for Graph2
                                    Graph2.Value++;
                                }
                            }
                        }

                        // Show finished
                        MessageBoxHelper.ShowMessage("The file '" + fileName + "' has been imported.", "Import Complete");
                    }
                }
                else
                {
                    // Show an error
                    MessageBoxHelper.ShowMessage("The selected import file could not be read.", "Import File Error");
                }
            }
            catch (Exception error)
            {
                // for debugging only
                DebugHelper.WriteDebugError("ImportLastNameButton_Click", this.Name, error);
            }
        }