Why porting data to a new system is a pain

You're going to get nailed no matter what you do when porting data to a new system.  I got nailed with how I linked to images in my old posts.

<img src="images/blargh.jpg" /> is a virtual path.  So if I have html page at http://betterthaneveryone.com/default.aspx, the image's path is different than if it was betterthaneveryone.com/sub1/default.aspx.  In the 2nd link, the path for the image would be sub1/images/blargh.jpg when the image is actually housed at /images/blargh.jpg.

With all that blargh explained, here is my mini app.  I used the SQL class from my drunktender project.  I used SqlParameters for the updating too.  I highly suggest using them whenever possible.  Could have refactored the code slightly more but not bad for 1 pass.  This took all of 10 minutes to write.  I think the UI took longer than the code to be honest.  Notice the checkbox I added for verifying I want to process the live data.  I didn't want a mishap.

I was going to try PowerShell for this but decided I shouldn't screw up my production database goofing off with new tech I haven't played with.

image

private DataSet dsTempObjects;
private int index = 0;

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    btnProcess.Enabled = (checkBox1.CheckState == CheckState.Checked);
}

private SQL getSqlObject()
{
    return new SQL(txtIP.Text, txtDatabase.Text, txtUser.Text, txtPassword.Text);
}

private void btnGetTestData_Click(object sender, EventArgs e)
{
    using (SQL sql = getSqlObject())
    {
        dsTempObjects = sql.ExecuteDataSetByQuery("select * from subtext_content where [text] like '%<img src=\"images/%'");
        index = 0;
        btnCheckNext.Enabled = true;
        btnCheckNext_Click(sender, e);
    }
}

private static string replaceBadText(string input)
{
    return Regex.Replace(input, "src=\"images", "src=\"/images");
}

private void populateInputOutput()
{
    if( dsTempObjects.Tables[0].Rows.Count > index )
    {
        string input = dsTempObjects.Tables[0].Rows[index]["text"].ToString();
        string output = replaceBadText(input);

        txtInputData.Text = input;
        txtOutputData.Text = output;
    }
    index++;
}

private void btnCheckNext_Click(object sender, EventArgs e)
{
    populateInputOutput();
}

private void btnProcess_Click(object sender, EventArgs e)
{
    using (SQL sql = getSqlObject())
    {
        dsTempObjects = sql.ExecuteDataSetByQuery("select * from subtext_content where [text] like '%<img src=\"images/%'");

        foreach (DataRow dr in dsTempObjects.Tables[0].Rows)
        {
            string newText = replaceBadText(dr["text"].ToString());
            string id = dr["ID"].ToString();

            sql.ExecuteNonQueryByQuery(
                "update subtext_content set [Text] = @newText where id = @Id",
                new SqlParameter("@newText", newText),
                new SqlParameter("@Id", id));
        }
    }

    MessageBox.Show("done");
}

No comments posted yet.

Post a Comment

Please add 3 and 2 and type the answer here:
  • About
  • Clint Rutkas: I build stuff that will eventually hurt me. I'm your average nerd.
  • Follow Clint on Twitter