这个 foreach 循环在测试时工作正常,只返回 5 行数据,但我很清楚它的编写方式有多糟糕,有没有更好的方法,可能使用 stringbuilder 更有效地重写它?
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstring"].ConnectionString);
SqlCommand comm = new SqlCommand("SELECT Title, StartDate FROM tblEvents JOIN eo_UserEventWatch ON eo_UserEventWatch.EventID=tblEvents.ID WHERE eo_UserEventWatch.UserID = @GUID ;", conn);
comm.Parameters.AddWithValue("GUID", userID);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
string result ="{ \"event\" :[";
foreach (DataRow dr in dt.Rows)
{
result += "{\"title\" : \"" + dr[0].ToString() + "\" , \"start\" : \"" + dr[1].ToString() +"\"} ,";
}
result = result.TrimEnd(',');
result += "] }";
return result;