ASP.NET equivalent of this PHP code (arrays)?
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)) {
$da开发者_Go百科te = strtotime($row["Date"])*1000;
$Data[] = array($date, $row["Data"]);
}
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#).
Is there a replacement for strtotime in aspx?
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],
...
]
First, how are you getting the data from the SQL database? I'll assume that you're getting it as a collection of DataRow
objects.
For storing the data, I would suggest using a list of tuples, as shown below.
List<Tuple<DateTime, string>> Data = new List<Tuple<DateTime, string>>();
// Somehow get a collection of rows into a results object
foreach (DataRow row in results)
{
DateTime dt = DateTime.Parse(row["Date"]);
Data.Add(new Tuple<DateTime, string>(dt, row["Data"]));
}
You can then access items in the list by index, and the individual fields as Item1
and Item2
:
DateTime dt = Data[0].Item1;
string itemData = Data[0].Item2;
精彩评论