douyong1285 2011-02-13 17:07
浏览 56
已采纳

ASP.NET相当于这个PHP代码(数组)?

I am mainly programming in PHP, but for my school project I have to use ASP.NET(C#). Now I have this code which works in PHP:

$Data = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $date = strtotime($row["Date"])*1000;
    $Data[] = array($date, $row["Data"]);
}
  1. As you can see it's a 2D array, which is expanding, but the inner array always has two parameters. Now I have searched on the web and haven't found how to declare a expanding array in ASP.NET(C#).

  2. Is there a replacement for strtotime in aspx?

  • 写回答

2条回答 默认 最新

  • doutan5844 2011-02-13 17:25
    关注

    Given it's now C# and the datatypes should come across through the reader, the following should work:

    // use a struct to store your retrieved information
    public struct Details
    {
      DateTime date;
      Object data;
    
      public Details(DateTime Date, Object Data)
      {
        date = Date;
        data = Data;
      }
    }
    
    // use List<T> to define a dynamically-sized list
    List<Details> dataArray = new List<Details>();
    while (reader.Read()) // query the database
    {
      // add the information to the list as we iterate over it
      dataArray.Add(new Details(reader.GetDateTime("Date"),reader["Data"]));
    }
    
    // your new expansive array: dataArray.ToArray();
    // converting an object to datetime: Convert.ToDateTime();
    

    This also assumes you're using a SqlDataReader (though you could use almost anything to retrieve the information from SQL).


    Update

    Given the comments below, the information is retrieved from a database and then output to JSON. Because of the new requirement I am going to change the store format a bit. This gives the flexibility to continue using the JavaScriptSerializer while outputting to a format javascript can understand.

    // make a list we can use
    List<Object[]> details = new List<Object[]>();
    
    // populate the data
    using (SqlConnection connection = new SqlConnection("<connection string>"))
    {
        SqlCommand command = new SqlCommand("<query string>");
        SqlReader reader = command.ExecuteReader();
    
        .. go through the database
        while (reader.Read())
        {
          DateTime date = reader.GetDateTime("Date");
          Double data = reader.GetDouble("Data");
    
          // convert the datetime to unix time
          // http://www.epochconverter.com/
          var epoc = (date.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
    
          details.Add(new Object[]{ epoc, data });
        }
    }
    
    // Now we serialize
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    String result = serializer.Serialize(details.ToArray());
    

    And here's what I receive from result (for format anyways):

    [
      [1297627668,0.7],
      [1297714068,1.1],
      [1297800468,0.1],
      ...
    ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!