Avatar of miksuFin
miksuFin
Flag for Finland asked on

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.
C#

Avatar of undefined
Last Comment
miksuFin

8/22/2022 - Mon
Russ Suter

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?
miksuFin

ASKER
Yes, I want replace all occurrences of the word in every table of my database.
My database is *.mdf.
Russ Suter

*.mdf is a filename filter, not a database. What are you using? Oracle, MySQL, Microsoft SQL Server?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
miksuFin

ASKER
Microsoft SQL Server
ASKER CERTIFIED SOLUTION
Russ Suter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
miksuFin

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

I didn't get good answer.
Russ Suter

The author requested advice. Advice was provided. If more advice was required the author could have requested additional information.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
miksuFin

ASKER
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!
miksuFin

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








                    }
                }
            }
        }
miksuFin

ASKER
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();
        }
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes