将PHP脚本转换为ASP.NET

时间:2010-06-11 09:35:04

标签: php asp.net

有谁知道如何在ASP.NET(VB.NET)中编写以下PHP?

foreach ($_GET['listItem'] as $position => $item) : 
  $sql[] = "UPDATE `table` SET `position` = $position WHERE `id` = $item"; 
endforeach; 

$ _GET ['listItem']中的数据如下所示:

项[] = 1&安培;项[] = 2及项[] = 3

1 个答案:

答案 0 :(得分:1)

我知道一点PHP,但我假设$ position是一个从零开始的索引。如果不是,那么你将交换声明位置变量的两行代码。评论以'character。

开头
'Request.Params accesses parameters from form posts, query string, etc.
Dim pairs = Request.Params("listItem").Split("&")

For i As Integer = 0 To pairs.Length - 1

  Dim token = pairs(i)
  Dim position = i.ToString()
  'string position = (i + 1).ToString();
  Dim id = Convert.ToInt32(token.Split("=")(1))
  Dim sql = "UPDATE table SET position = " + position + " WHERE [id] = " + id.ToString()
  'This line does the same thing, a bit more eloquently and efficiently
  'Dim sql = String.Format("UPDATE table SET position = {0} WHERE [id] = {1}", position, id.ToString())

Next

我首先在C#中这样做,因为我没有注意到你说过VB.net。所以这是C#版本。以为我不妨留下它。 我把它做了一点罗嗦,以展示C#的一些细微差别和清晰度。

// Request.Params accesses parameters from form posts, query string, etc.
string[] pairs = Request.Params["listItem"].Split('&');
for (int i = 0; i < pairs.Length; i++)
{
  string token = pairs[i];
  string position = i.ToString();
  // string position = (i + 1).ToString();
  int id = Convert.ToInt32(token.Split('=')[1]);
  string sql = "UPDATE table SET position = " + position + " WHERE [id] = " + id;
  // This line does the same thing, a bit more eloquently and efficiently
  //string sql = String.Format("UPDATE table SET position = {0} WHERE [id] = {1}", position, id.ToString());
}