C# ASP.NET等效于此PHP代码(数组)?
我主要用PHP编程,但对于我的学校项目,我必须使用ASP.NET(C#)。现在我有了在PHP中工作的代码:C# ASP.NET等效于此PHP代码(数组)?,c#,php,asp.net,C#,Php,Asp.net,我主要用PHP编程,但对于我的学校项目,我必须使用ASP.NET(C#)。现在我有了在PHP中工作的代码: $Data = array(); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $date = strtotime($row["Date"])*1000; $Data[] = array($date, $row["Data"]); } 如您所见,它是一个二维数组,正在扩展,但内部数组始终有两个参数。现在我
$Data = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$date = strtotime($row["Date"])*1000;
$Data[] = array($date, $row["Data"]);
}
首先,您如何从SQL数据库获取数据?我假设您将它作为
DataRow
对象的集合来获取
为了存储数据,我建议使用元组列表,如下所示
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"]));
}
考虑到它现在是C#和数据类型,以下应该可以工作:
// 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();
我同意这个看起来很好。但是我可以使用JavaScriptSerializer吗?因为我必须将其序列化为JSON?@JernejJerin:您应该能够,但您可能希望在类中定义对象结构并反序列化到该结构。否则,我相信,默认情况下,您将得到一系列项,并且(取决于您的结构)它们可能是您想要的,也可能不是。因此,这个Convert.ToDateTime等于upper solution DateTime.Parse?@JernejJerin:是的,但它是在SQL读取器中实现的。您也可以使用Convert.ToDateTime,但如果信息无效/不可转换,则DateTime.Parse将异常输出。如果你想让DateTime.TryParse通过无效输入继续运行,你也可以使用它(如果可以或不能转换,函数将返回true/false)。@Brad Christie:我实现了你的想法。上面的代码是我在Data.aspx.cs中实现的,然后我调用函数(ReturnArray()),该函数遍历读取器并返回dataArray.ToArray()。我从以下参数调用了该函数:string arrayJson=serializer.Serialize(ReturnArray())。但是它返回空结果:[{},{}],在这个例子中是:dataArray.Add(newdetails(DateTime.Parse(“2010-05-01 00:00:00”),5);添加(新的细节(DateTime.Parse(“2010-05-01 00:10:00”),6)@杰林:啊,你想把你的头发染成红色,是吗?应该说得更清楚一点。;-)给我一点时间,我会更新我的答案(我会留下另一条评论,让你知道它已经更新了)。@JernejJerin:什么在消耗JSON数据?编码结果中的
{“date”:“date(156266026)}
值是否可接受?
// 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();
// 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());
[
[1297627668,0.7],
[1297714068,1.1],
[1297800468,0.1],
...
]