C# Replacing words in database

I want replace words in database.

There is 2 textboxes and 1 button on the form.
Textbox1 is "Find the word".
Textbox 2 is "Replace the word with this word"
Button is "Find and replace event"

Can you give me some advices to write the code.
miksuFinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterSenior Software DeveloperCommented:
This is a VERY broad question.
Do you want to replace ALL occurrences of the word in EVERY table of your database?
What database are you using?
miksuFinAuthor Commented:
Yes, I want replace all occurrences of the word in every table of my database.
My database is *.mdf.
Russ SuterSenior Software DeveloperCommented:
*.mdf is a filename filter, not a database. What are you using? Oracle, MySQL, Microsoft SQL Server?
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

miksuFinAuthor Commented:
Microsoft SQL Server
Russ SuterSenior Software DeveloperCommented:
I assume you're already connecting to the database now.

I'd do it in 2 parts.

The first is to request from the database a list of all tables using the following query:
SELECT * FROM information_schema.tables WHERE [TABLES].[TABLE_TYPE] = 'BASE TABLE'

Open in new window

Then, iterate through the returned DataTable and query the TABLE_CATALOG and TABLE_NAME columns.

You would then need to query each table like this:
SELECT *
FROM [TABLE_CATALOG from above query].INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'[TABLE_NAME]'

Open in new window

You will likely want to filter the result on the DATA_TYPE column since you will only want to affect changes to columns of type text, ntext, char, nchar, varchar, or nvarchar.

Finally, for each column from the above query, you can perform a standard string or Regex Replace.

Depending on the size and complexity of your database this might take a while.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
miksuFinAuthor Commented:
I've requested that this question be deleted for the following reason:

I didn't get good answer.
Russ SuterSenior Software DeveloperCommented:
The author requested advice. Advice was provided. If more advice was required the author could have requested additional information.
miksuFinAuthor Commented:
Hello,
I'm sorry if I've been unclear.

Here is the form
Find-and-replace.png
1.You choose the item from the combobox. The combobox includes column fields such as Forename, Surname,Address,City from the datatable.
2.You put  word 'Newport' to the upper textbox.
3. You put  word 'Oldport' to the lower textbox.
4. You click the Replace button. The application replaces all 'Newport' words in City field to 'Oldport' words in City field in whole datatable.

Thanks for your attention!
miksuFinAuthor Commented:
Building and debugging of this code manage, but word doesn't update.

 private void Replace_Click(object sender, EventArgs e)
        {

            int item1 = ComboBox.SelectedIndex;
           // String item2 = this.textBox1.Text;
            String item3 = this.textBox2.Text;

            using (SqlConnection sqlConn = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB; Initial Catalog=C:\\xx\\xx\\xx\\Mydatabase.mdf "))

            using (DataTable table = new DataTable("Information"))
            {
                using (sqlConn)
                {
                    using (SqlCommand cmd = sqlConn.CreateCommand())
                    {
                        sqlConn.Open();

                        switch (item1)
                        {
                            case 1:
                                cmd.CommandText = "SELECT Forename FROM Information WHERE Forename=        'this.textBox1.Text'";
                                cmd.CommandText = "UPDATEInformation SET Forename=@fn";
                                cmd.Parameters.AddWithValue("@fn", item3);
                                break;
                            case 2:
                            case 3:
                                //do some stuff
                                break;
                            case 4:
                            case 5:
                            case 6:
                                //do some different stuff
                                break;
                            default:
                                //default stuff
                                break;
                        }

                        sqlConn.Close();








                    }
                }
            }
        }
miksuFinAuthor Commented:
I changed code to this:

private DataTable ReplaceDB()
        {

            using (SqlConnection sqlConn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename= C:\\xx\\xx\\xx\\xx\\xx\\mydatabase.mdf ;Integrated Security=True"))
            {
                using (DataTable table = new DataTable("Information"))
                {
                    using (sqlConn)
                    {

                        int item1 = ComboBox.SelectedIndex;

                        switch (item1)
                        {
                            case 1:
                                string str1 = "SELECT Forename FROM Information WHERE Name LIKE '%' =  @fn = '"+textBox1.Text+"'";
                                SqlCommand xp1 = new SqlCommand(str1, sqlConn);
                                xp1.CommandText = "UPDATE Information SET Forename=@fn";
                                xp1.Parameters.AddWithValue("@fn", SqlDbType.NVarChar).Value = textBox2.Text;

                                sqlConn.Open();
                                xp1.ExecuteNonQuery();
                                SqlDataAdapter da = new SqlDataAdapter();
                                da.SelectCommand = xp1;
                                da.Fill(table);
                                sqlConn.Close();

                                break;


                                  case 2:
                                //SOME CODE
                                //break;

                                  case 3:
                                  //SOME CODE
                                //break;

                                  case 4:
                                 //SOME CODE
                                //break;
                           
                              }



                         

     
                        }

                        return table;
                    }
                }
            }
        }


        private void Replace_Click(object sender, EventArgs e)
        {
            DataTable table = ReplaceDB();
        }
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.