2013-11-27 145 views
0

我是新來的應用程序塊。無法將類型'System.Data.DataSet'隱式轉換爲'System.Collections.Generic.List <CalendarEvent>''

我想從數據庫中獲取數據。以下是代碼捕捉。

JsonResponse.ashx:

public void ProcessRequest(HttpContext context) 
{ 
    HttpContext _context = HttpContext.Current; 

    context.Response.ContentType = "application/json"; 
    int user_id = Convert.ToInt32(HttpContext.Current.Session["userid"]); 
    DateTime start = new DateTime(1970, 1, 1); 
    DateTime end = new DateTime(1970, 1, 1); 

    start = start.AddSeconds(double.Parse(context.Request.QueryString["start"])); 
    end = end.AddSeconds(double.Parse(context.Request.QueryString["end"]));  
    String result = String.Empty; 

    result += "["; 

    List<int> idList = new List<int>(); 
    foreach (CalendarEvent cevent in EventDAO.getEvents(start, end, user_id)) 
    { 
     result += convertCalendarEventIntoString(cevent); 
     idList.Add(cevent.id); 
    } 

    if (result.EndsWith(",")) 
    { 
     result = result.Substring(0, result.Length - 1); 
    } 

    result += "]"; 
    //store list of event ids in Session, so that it can be accessed in web methods 
    context.Session["idList"] = idList; 

    context.Response.Write(result); 
} 

private String convertCalendarEventIntoString(CalendarEvent cevent) 
{ 
    String allDay = "true"; 
    if (ConvertToTimestamp(cevent.start).ToString().Equals(ConvertToTimestamp(cevent.end).ToString())) 
    { 

     if (cevent.start.Hour == 0 && cevent.start.Minute == 0 && cevent.start.Second == 0) 
     { 
      allDay = "true"; 
     } 
     else 
     { 
      allDay = "false"; 
     } 
    } 
    else 
    { 
     if (cevent.start.Hour == 0 && cevent.start.Minute == 0 && cevent.start.Second == 0 
      && cevent.end.Hour == 0 && cevent.end.Minute == 0 && cevent.end.Second == 0) 
     { 
      allDay = "true"; 
     } 
     else 
     { 
      allDay = "false"; 
     } 
    } 
    return "{" + 
       "id: '" + cevent.id + "'," + 
       "title: '" + HttpContext.Current.Server.HtmlEncode(cevent.title) + "'," + 
       "start: " + ConvertToTimestamp(cevent.start).ToString() + "," + 
       "end: " + ConvertToTimestamp(cevent.end).ToString() + "," + 
       "allDay:" + allDay + "," + 
       "user_id:" + cevent.user_id + "," + 
       "description: '" + HttpContext.Current.Server.HtmlEncode(cevent.description) + "'" + 

       "},"; 
} 

DA:

提供SQLHelper:

public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 
{ 
    //create a command and prepare it for execution 
    SqlCommand cmd = new SqlCommand(); 
    cmd.CommandTimeout = 120; 
    PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters); 

    //create the DataAdapter & DataSet 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    DataSet ds = new DataSet(); 

    //fill the DataSet using default values for DataTable names, etc. 
    da.Fill(ds); 

    // detach the SqlParameters from the command object, so they can be used again.   
    cmd.Parameters.Clear(); 

    //return the dataset 
    return ds;      
} 

我得到錯誤:

Cannot implicitly convert type 'System.Data.DataSet' to 'System.Collections.Generic.List'.

我無法理解問題所在。

+1

'ExecuteDataset'返回'DataSet'你立即試圖返回的''從列表getEvents' '。那就是問題所在。使用虛擬屬性,因爲我不知道 – GSerg

回答

0

getEvents方法中,您需要遍歷數據集中的記錄並填寫您將在此方法中返回的列表。

var dataset = SqlHelper.ExecuteDataset(connectionString,CommandType.StoredProcedure, "GetData", sqlParam); 

foreach (var row in ds.Tables["FooTable"].Rows) 
{ 
    events.Add(new CalendarEvent(...)); 
} 

return events; 
0

這是因爲你試圖返回一個數據集作爲列表,它不是。 您需要將數據集轉換爲列表。一種可能的解決辦法是改變getEvents方法是這樣的 - >

public static List<CalendarEvent> getEvents(DateTime start, DateTime end, int user_id) 
{ 

    List<CalendarEvent> events = new List<CalendarEvent>(); 

    SqlParameter[] sqlParam = new SqlParameter[3]; 
    sqlParam[0] = new SqlParameter("@start", start); 
    sqlParam[1] = new SqlParameter("@end", end); 
    sqlParam[2] = new SqlParameter("@user_id", user_id); 
    var ds = SqlHelper.ExecuteDataset(connectionString,CommandType.StoredProcedure, "GetData", sqlParam); 
    return ds.Tables[0].AsEnumerable().Select(datarow => new CalendarEvent{ Title = datarow.Field<string>("Title), /*the rest of your params*/}).ToList(); 
} 
0

你的問題是這樣的一段代碼:

public static List<CalendarEvent> getEvents(DateTime start, DateTime end, int user_id) 
{ 
    List<CalendarEvent> events = new List<CalendarEvent>(); 
    SqlParameter[] sqlParam = new SqlParameter[3]; 
    sqlParam[0] = new SqlParameter("@start", start); 
    sqlParam[1] = new SqlParameter("@end", end); 
    sqlParam[2] = new SqlParameter("@user_id", user_id); 
    return SqlHelper.ExecuteDataset(connectionString,CommandType.StoredProcedure, "GetData", sqlParam); 
} 

你定義了這個方法的類型List<CalenderEvent>但你回來一個DataSet。 我不知道哪些數據表包含在您的數據集中,但我認爲有一個表示您的calenderevents。

這意味着您需要從數據集中提取所需的數據並將其列出。假設有數據集中的一個表的新方法將是這個樣子:

public static List<CalendarEvent> getEvents(DateTime start, DateTime end, int user_id) 
{ 
    List<CalendarEvent> events = new List<CalendarEvent>(); 
    SqlParameter[] sqlParam = new SqlParameter[3]; 
    sqlParam[0] = new SqlParameter("@start", start); 
    sqlParam[1] = new SqlParameter("@end", end); 
    sqlParam[2] = new SqlParameter("@user_id", user_id); 


    var data = SqlHelper.ExecuteDataset(connectionString,CommandType.StoredProcedure, "GetData", sqlParam); 
    events = ds.Tables[0].AsEnumerable().Select(r => new CalenderEvent 
             { 
              //using dummy properties because I dont know 
              //your class 
              Property1 = r.Field<string>("Column1"), 
              Property2 = r.Field<string>("column2"), 
              //... 
             }).ToList(); 
    return events; 
} 
+0

。選擇(R =>新CalenderEvent {// // 類 Property1 = r.Field ( 「列1」), Property2 = r.Field ( 「列2」 ), // ... })。ToList(); – Monica

+0

你想告訴我什麼莫妮卡? – Marco

+0

=>運算符不支持....我在這部分使用vs 2005.error,事件= ds.Tables [0] .AsEnumerable()。選擇(r =>新CalenderEvent { //使用虛擬屬性,因爲我不知道 //您的班級 Property1 = r.Field (「Column1」), })。ToList(); – Monica

相關問題