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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)