Batch Data replacement assistant V1.0 released

Source: Internet
Author: User

This article from: http://www.cnblogs.com/huangzelin/archive/2011/10/02/2198047.html

 

Some time ago, the website was infected with Trojans, and many text fields in the database table were added with a js script. After fixing the program vulnerability, we began to clean up the injected data. Some online methods were used to write a stored procedure to clean one table and one table one by one. This method is cumbersome, and it is difficult for people who do not know the database. I was tempted to write a small program. After two days of hard work, I finally met you. I hope you will give more comments to your children's shoes. After that, let's go to the interface first, ^... ^

Now let's talk about the development idea of this small program.
Step 1: Use the sp_helpdb system stored procedure to obtain the names of all databases in SqlServer.

View Code

# Region test database connection and display the Database List
/// <Summary>
/// Test the database connection and display the Database List
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void btnTest_Click (object sender, EventArgs e)
{
This. btnTest. Enabled = false;
SaveConfig ();

ConfigInfo. Server = this.txt IP. Text. Trim ();
ConfigInfo. DataBase = "master ";
ConfigInfo. UID = this.txt UID. Text. Trim ();
ConfigInfo. Pwd = this.txt Pwd. Text. Trim ();

Try
{
DataTable dt = Data. SqlHelper. ExecuteDataset (ConfigInfo. getConnect (), CommandType. Text, "sp_helpdb"). Tables [0];

This. cmbDataBaseList. DataSource = dt;
This. cmbDataBaseList. DisplayMember = "name ";
This. cmbDataBaseList. SelectedIndex = 0;
This. cmbDataBaseList. DropDownStyle = ComboBoxStyle. DropDownList;

This. ExecuteFilterBtn. Enabled = true;
}
Catch (Exception ex)
{
This. ExecuteFilterBtn. Enabled = false;
MessageBox. Show (string. Format ("error: {0 }! ", Ex. Message)," Error prompt ", MessageBoxButtons. OK, MessageBoxIcon. Error );
}
Finally
{
This. btnTest. Enabled = true;
}
}
# Endregion

Step 2: When you select a database, you can obtain all the table information in the database. You can use the following SQL statement to query the information.
Select [name] from sysobjects where xtype = 'U' order by [name] asc

View Code

# Region when selecting a different database, read the table information of the database
/// <Summary>
/// Read the table information of the database when different databases are selected
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void combobox#selectedindexchanged (object sender, EventArgs e)
{
This. chkboxTableList. Items. Clear ();
Configinfo. Database = (datarowview) This. cmbdatabaselist. selecteditem) ["name"]. tostring ();
Dataset DS = data. sqlhelper. executedataset (configinfo. getconnect (), commandtype. text, "select [name] From sysobjects where xtype = 'U' order by [name] ASC ");

Foreach (datarow row in DS. Tables [0]. Rows)
{
This. chkboxtablelist. Items. Add (row ["name"]. tostring ());
}
}
# Endregion

Step 3: Click the replace button to obtain the information of the selected table, traverse the row and column information in the table, and search and replace the information.

View Code

# Region performing batch replacement
/// <Summary>
/// Perform the batch Replacement Operation
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void executefilterbtn_click (Object sender, eventargs E)
{
Saveconfig ();
Total = 0;
If (this. chkboxtablelist. checkedindices. Count = 0) return; // No table is selected
If (this.txt searchkey. Text. Trim () = "")
{
Dialogresult result = MessageBox. Show ("the current search content is empty. Are you sure you want to perform this operation? "," Prompt ", messageboxbuttons. yesno, messageboxicon. Question, messageboxdefaultbutton. button1 );
If (result = dialogresult. No) return;
}

This. executefilterbtn. Enabled = false;

List <tableinfo> tablist = new list <tableinfo> ();
String searchstring = this.txt searchkey. Text. Trim () = ""? "": This.txt searchkey. text;
String replacestring = this.txt replacestr. text;
Keytype KT = This. chkisregex. Checked = true? Keytype. RegEx: keytype. text;
Bool isRegex = this. chkIsRegex. Checked;

// Obtain the basic information of the selected table and add it to the set
Foreach (int index in this. chkboxTableList. CheckedIndices)
{
String tabName = this. chkboxTableList. Items [index]. ToString ();
TableInfo tInfo = FilterInfo. initTableInfo (tabName );
If (tInfo = null)
{
Continue;
}
TabList. Add (tInfo );
}

Try
{
If (tabList. Count = 0) return; // No data table that meets the detection Condition

PBar1.Visible = true;
PBar1.Minimum = 1;
PBar1.Maximum = tabList. Count;
PBar1.Value = 1;
PBar1.Step = 1;

// The data to be replaced in the cyclic filtering table
Foreach (TableInfo info in tabList)
{
FilterInfo. Execute (info, searchString, replaceString, kt );
Pbar1.20.mstep (); // progress bar
}
}
Catch (Exception ex)
{
MessageBox. Show (string. Format ("exception: {0}", ex. Message), "Error", MessageBoxButtons. OK, MessageBoxIcon. Error );
Return;
}
Finally
{
This. ExecuteFilterBtn. Enabled = true;
}

MessageBox. Show (string. Format ("data has been replaced. A total of {0} rows of data have been modified! ", Total)," message ", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
# Endregion

 

The above is the general idea. For details, refer to the source code.

 

With some operations, I hope you can see more clearly.

This is the injected data. Of course, there are actually differences.

Write the search content and enable the regular expression matching function.

Haha, the data has finally recovered !!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.