SQL Server bulk Data Replacement Assistant V1.0 Release _ Practical Tips

Source: Internet
Author: User
This method is cumbersome to operate, and is generally not very easy to understand the database of people. In the germination of a small program to write the idea, after two days of tossing this small software finally and you meet, I hope that children's shoes more to give some advice. After all that, let's start with the interface, ^. ^

Now let's talk about the development of this small program.
Step one: Get all the database names in SQL Server through the sp_helpdb system stored procedure.

Copy Code code as follows:

#region Test the database connection and display the list of databases
<summary>
Test the database connection and display the list of databases
</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.txtIP.Text.Trim ();
Configinfo.database = "Master";
Configinfo.uid = This.txtUID.Text.Trim ();
Configinfo.pwd = This.txtPwd.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 hint", MessageBoxButtons.OK, Messageboxicon.error);
}
Finally
{
This.btnTest.Enabled = true;
}
}
#endregion


The second step: When a database is selected to get all the table information inside the database, through the following SQL statement can be queried.
Select [Name] from sysobjects where xtype= ' u ' ORDER BY [name] ASC

Copy Code code as follows:

#region to read table information for a database when you select a different database
<summary>
Read table information for a database when you select a different database
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void ComboBox1_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 three: When you click on the replacement button, get the information of the selected table, and traverse the row and column information in the table, and find the replacement.

Copy Code code as follows:

#region Perform bulk substitution operations
<summary>
Perform a bulk substitution 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 tables are selected
if (This.txtSearchKey.Text.Trim () = "")
{
DialogResult result = MessageBox.Show (the current lookup is empty, confirm this action?) "," hint ", Messageboxbuttons.yesno, Messageboxicon.question, Messageboxdefaultbutton.button1);
if (result = = dialogresult.no) return;
}

This. executefilterbtn.enabled = false;

list<tableinfo> tablist = new list<tableinfo> ();
string searchstring = This.txtSearchKey.Text.Trim () = ""? " ": This.txtSearchKey.Text;
string replacestring = This.txtReplaceStr.Text;
KeyType kt = this.chkIsRegex.Checked = = true? KeyType.Regex:KeyType.Text;
bool Isregex = this.chkIsRegex.Checked;

Get the basic information of the selected table and add it to the collection
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 matches the test

Pbar1.visible = true;
Pbar1.minimum = 1;
Pbar1.maximum = Tablist.count;
Pbar1.value = 1;
Pbar1.step = 1;

Loop filter data to be replaced in a table
foreach (Tableinfo info in tablist)
{
Filterinfo.execute (info, searchstring, replacestring, kt);
Pbar1.performstep (); 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 replacement completed, a total of {0} rows of data has been modified!") ", total)," message ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
#endregion

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

With some action screenshot, I hope you can see more clearly.

This is the injected data, of course, the actual difference.

Write the lookup content and enable the regular matching feature.

Haha, the data finally restore the original appearance!!
SOURCE program Download Address

Related Article

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.