C# Replacing words in database

miksuFin
miksuFin used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Russ SuterSenior Software Developer

Commented:
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?

Author

Commented:
Yes, I want replace all occurrences of the word in every table of my database.
My database is *.mdf.
Russ SuterSenior Software Developer

Commented:
*.mdf is a filename filter, not a database. What are you using? Oracle, MySQL, Microsoft SQL Server?
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Author

Commented:
Microsoft SQL Server
Senior Software Developer
Commented:
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.

Author

Commented:
I've requested that this question be deleted for the following reason:

I didn't get good answer.
Russ SuterSenior Software Developer

Commented:
The author requested advice. Advice was provided. If more advice was required the author could have requested additional information.

Author

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!

Author

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();








                    }
                }
            }
        }

Author

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();
        }

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial