0

I am currently working on a patching system in C# and I have came across a small complication. I am using MySQL to store an archive for my update list. The patching system then detects the version of program, and downloads every patch after that version. Though I just started learning how to use MySQL in C# so i'm not sure how to do, or call a lot of the functions needed. What I want to do is use foreach to call all values in the "version" column/row, then use a while loop to check against current version and new version until they are the same. I just cant seem to figure out how to use the two together and can't find any references.

using (SqlCon = new MySqlConnection(connString))
{
    SqlCon.Open();
    string command = "SELECT * FROM version ORDER BY version";
    MySqlCommand GetLatestVersion = new MySqlCommand(command, SqlCon);

    using (MySqlDataReader DR = GetLatestVersion.ExecuteReader())
    {
        while (DR.Read())
        {
            foreach(DataTable i in DR)
            {
                while(v1 < v2)
                {
                    string LatestVersion = Convert.ToString(DR.GetValue(1));
                    string WebURL = Convert.ToString(DR.GetValue(2));
                    update.DownloadProgressChanged += new DownloadProgressChangedEventHandler(download);
                    update.DownloadFileCompleted += new AsyncCompletedEventHandler(extration);
                    update.DownloadFileAsync(new Uri(WebURL), tempFilePath + "patch" + Latest_Version + ".zip");
                }
            }
        }
    }
}
SqlCon.Close();

I would greatly appreciate any help.

2 Answers 2

3

just remove the inner foreach and you are good to go.

Sign up to request clarification or add additional context in comments.

Comments

1

First thing is you don't need the SqlCon.Close(); at the end. At the end of the using block, the object is disposed of (the point of a using block).

You can modify your select statement to only select versions greater than your program's current version. This way, any records selected should be processed/downloaded. (I put the version in quotes in the SQL statement below because your code indicates that it's a string. You're probably better off specifying this value as numeric for sorting/comparison purposes, though.)

//for readability, I changed the variable name to myProgramsVersion

using (SqlCon = new MySqlConnection(connString))
{
    SqlCon.Open();
    string command = "SELECT * FROM version where version > '" + myProgramsVersion + "' ORDER BY version";
    MySqlCommand GetLatestVersion = new MySqlCommand(command, SqlCon);

    using (MySqlDataReader DR = GetLatestVersion.ExecuteReader())
    {
        while (DR.Read())
        {
            string LatestVersion = Convert.ToString(DR.GetValue(1));
            string WebURL = Convert.ToString(DR.GetValue(2));
            update.DownloadProgressChanged += new DownloadProgressChangedEventHandler(download);
            update.DownloadFileCompleted += new AsyncCompletedEventHandler(extration);
            update.DownloadFileAsync(new Uri(WebURL), tempFilePath + "patch" + Latest_Version + ".zip");
        }
    }
}

3 Comments

I actually never had tried to use greater than or less than signs in an sql query. I didn't know it was possible or I would of done that instead of trying to do the for loop. I'm using the assembly version to check against the database. I was going to use the file version, but when i try it say it's to long to be converted into Version(); I got one last question if you don't mind me asking. Is it possible to check against the assembly version of another program? I found how to check the file version with FileVersionInfo, but i does no good when i can't get the value with an error
I assume you're talking about the major/minor/build/revision numbers. You could create for separate columns in the database for each then load them into a Version object and use its CompareTo() method. That would preclude my suggestion offered above (unless you want to write a REALLY messy SQL statement). Another option is to convert the assembly version to an integer as specified in the question linked at the end of this comment. You can store this in the database and do a simple > on integers. As that question points out, though, this isn't foolproof. bit.ly/sCyL6
Ah ok well I got it figured out. Now just need to figure out how to get the program to only process one thing at a time. It keeps trying to process everything at once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.