C# Foreach 与 MySQL

发布于 2024-12-18 22:19:05 字数 1235 浏览 0 评论 0原文

我目前正在使用 C# 开发一个修补系统,并且遇到了一个小问题。我正在使用 MySQL 来存储更新列表的存档。然后,补丁系统会检测程序的版本,并下载该版本之后的每个补丁。虽然我刚刚开始学习如何在 C# 中使用 MySQL,所以我不知道该怎么做,或者调用很多需要的函数。我想要做的是使用 foreach 调用“版本”列/行中的所有值,然后使用 while 循环检查当前版本和新版本,直到它们相同。我似乎无法弄清楚如何将两者一起使用,也找不到任何参考资料。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好倦 2024-12-25 22:19:05

只需删除内部的 foreach 就可以了。

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

筑梦 2024-12-25 22:19:05

首先,您不需要最后的 SqlCon.Close(); 。在 using 块的末尾,对象被释放(using 块的点)。

您可以修改 select 语句以仅选择高于程序当前版本的版本。这样,任何选择的记录都应该被处理/下载。 (我在下面的 SQL 语句中将版本放在引号中,因为您的代码表明它是一个字符串。不过,出于排序/比较的目的,最好将此值指定为数字。)

//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");
        }
    }
}

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");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文