Link to home
Start Free TrialLog in
Avatar of miksuFin
miksuFinFlag 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.
Avatar of Russ Suter
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?
Avatar of miksuFin

ASKER

Yes, I want replace all occurrences of the word in every table of my database.
My database is *.mdf.
*.mdf is a filename filter, not a database. What are you using? Oracle, MySQL, Microsoft SQL Server?
Microsoft SQL Server
ASKER CERTIFIED SOLUTION
Avatar of Russ Suter
Russ Suter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've requested that this question be deleted for the following reason:

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

Here is the form
User generated image
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!
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();








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