C# Foreach 与 MySQL
我目前正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需删除内部的
foreach
就可以了。just remove the inner
foreach
and you are good to go.首先,您不需要最后的
SqlCon.Close();
。在using
块的末尾,对象被释放(using
块的点)。您可以修改 select 语句以仅选择高于程序当前版本的版本。这样,任何选择的记录都应该被处理/下载。 (我在下面的 SQL 语句中将版本放在引号中,因为您的代码表明它是一个字符串。不过,出于排序/比较的目的,最好将此值指定为数字。)
First thing is you don't need the
SqlCon.Close();
at the end. At the end of theusing
block, the object is disposed of (the point of ausing
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.)