我需要以 JSON 格式返回从 MS SQL 2008 表中选择的名称和电子邮件。这是我到目前为止所得到的:
public class RaumHelper
{
internal static string[] Raum()
{
List<string> raumObject = new List<string>();
using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;"))
using (SqlCommand cmd = new SqlCommand(@"SELECT NAME, EMAIL FROM RAUM", con))
{
con.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
if (rdr["NAME"] != DBNull.Value && rdr["EMAIL"] != DBNull.Value)
{
raumObject.Add(rdr["NAME"].ToString());
raumObject.Add(rdr["EMAIL"].ToString());
}
}
}
}
return raumObject.ToArray();
}
}
我想以 json 格式而不是数组返回它。
它应该看起来像这样或类似的:
{"d":[{
"Name": "Hans",
"Email": "bla@blub.de"
}]}