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#
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?
Here is the form
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.
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();
Do you want to replace ALL occurrences of the word in EVERY table of your database?
What database are you using?