2012-04-30 3 views
0

을 사용하여 wcf ria 서비스를 사용하여 RSS 데이터를 데이터베이스에 삽입합니다. 많은 게시물을 읽었지 만 내 answer.my 질문을 찾을 수 없습니다. 내 silverlight 프로젝트에서 temp, 상태 및 날짜와 같은 날씨 데이터를 얻고 싶습니다. yahoo 날씨에서 rss.so iusing 웹 클라이언트 및 그것의 DownloadStringAsync 및 DownloadStringCompleted에서 data.also i 모델을 폴더에 내 서버에서 프레 젠 테이션 모델을 만들었습니다 (내 서비스에서 그것을 사용하고 싶었던 beacause을 변경하여 countiniously 내 데이터베이스에 저장) 내 DownloadStringCompleted 이벤트 처리기 그래서 나는이 같은 짓을 한 :프리젠 테이션 모델

void xmlclient_DownloadStringCompleted(object sender,DownloadStringCompletedEventArgs e) 
    { 
     XNamespace yweather = "http://xml.weather.yahoo.com/ns/rss/1.0"; 
     if (e.Error == null) 
     { 
      XElement x = XElement.Parse(e.Result); 
      weatherquery1 = 
       from items in x.Descendants("item") 

       select new BusinessApplication1.Web.Models.WeatherConditionModel 
       { 

       PubDate = items.Element(yweather +"condition").Attribute("date").Value, 
       Status = items.Element(yweather + "condition").Attribute("text").Value 
       }; 
      } 

      } 

이 내 뷰 모델이며 내가 모든을 남기고는 다에 결과를 볼 수 있습니다 또한 데이터를 얻을 수 있습니다 그것을 테스트 tagrid 또는 listbox. inow 나는 내 database.i에 데이터를 저장하고 싶습니다. 버튼이나 command.i가 아닌 자동으로 수행하고 싶습니다. 데이터를 항상 읽고 원하는 5 분마다 데이터베이스에 저장하고 싶습니다.

private void MapwcModel(WeatherConditionTable wctable, WeatherConditionModel wcPM) 
    { 
     wctable.Status = wcPM.Status; 
     wctable.PubDate = wcPM.PubDate; 
     wctable.WeatherConditionID = wcPM.WeatherConditionID; 

    } 

    [Insert] 
    [Invoke] 
    public void InsertWeatherConditionData(WeatherConditionModel WeatherConditionData) 
    { 
     WeatherConditionTable wc = WeatherConditionContext.WeatherConditionTables.CreateObject(); 
     MapwcModel(wc, WeatherConditionData); 
     wc.Status = WeatherConditionData.Status; 
     wc.PubDate = WeatherConditionData.PubDate; 
     WeatherConditionContext.WeatherConditionTables.AddObject(wc); 
     WeatherConditionContext.SaveChanges(); 


    } 

내 데이터 가져 오기 :

지금
 public IQueryable<WeatherConditionModel> GetWeatherConditionData() 
      { 
     return from p in this.WeatherConditionContext.WeatherConditionTables 
       select new WeatherConditionModel 
       { 
        WeatherConditionID = p.WeatherConditionID, 
        Status = p.Status, 
        PubDate = p.PubDate, 

       }; 
      } 

내가 data.i가이 쓴 저장을 강제하는 방법을 모른다 서비스와 내가 지정 내가 나 자신을 형성 할 수 삽입을 만들 내 iewmodel하지만 작동하지 않았다 :

foreach (BusinessApplication1.Web.Models.WeatherConditionModel el in weatherquery1) 

       { 
       WeatherConditionDomainContext context = new WeatherConditionDomainContext(); 
       EntityQuery<BusinessApplication1.Web.Models.WeatherConditionModel> weatherLoadQuery = context.GetWeatherConditionDataQuery(); 
       context.Load<BusinessApplication1.Web.Models.WeatherConditionModel>(weatherLoadQuery); 
       context.SubmitChanges(delegate(SubmitOperation operation) 
       { 
        if (operation.HasError) 
        { 
         operation.MarkErrorAsHandled(); 
        } 
       }, null); 
       } 

나는 삽입 방법을 강제로 작동시키는 법을 모른다. 누군가 내가 틀린 곳에서 말해줘? 나는 어딘가에있다는 것을 알고있다. 안부

+0

WeatherConditionDomainContext *를 작성하여 변경 사항을 추가합니다 (루프 밖에서 한 번만 ...!) * 컨텍스트 테이블에 항목을 추가하십시오. 지금까지는 세이브 코드가로드되고 SubmitChanges가 발생하지만 실제로 컨텍스트의 테이블에 변경 사항이 추가되지 않으므로 아무 것도 저장되지 않습니다.덕분에 –

+0

. 하지만 먼저, 시간 간격의 변화를 저장하지 말라고 했으므로 테스트를 위해 적어도 하나의 데이터를 저장하고 싶지만 데이터베이스는 비어 있습니다. 두 번째로 엔트리를 컨텍스트 테이블에 추가하면 뷰 모델을 의미합니까? 제발 코드에 제발 보여 주시겠습니까? 셋째, 정확한 형식으로 제 삽입입니까? 감사합니다 – azad6026

+0

아무도 도와 드릴 수 없습니까? 나는 무엇을 해야할지 모르겠다? 이 부분에 대한 – azad6026

답변

0

: Silverlight 4 Unleased- 13 장 - 위대한 : Laurent Bugnion. 첫째

, 그냥 내 쿼리 출력의 홀더로 사용하는 클래스입니다 .IT 프레젠테이션 모델 (여러 테이블의 데이터를 저장해야 할 때 사용되는 프리젠 테이션 모델 ID)로 WeatherConditionModel를 사용할 필요가 없다. 둘째로, 서비스에서 Insert 메소드를 전혀 변경할 필요가 없습니다. (여기서는 하나의 테이블에 데이터를 저장하고 싶기 때문에) 그래서 엔티티 모델에 서비스를 만들고 빌드하십시오.이 방법으로 빌드하면 당신이보기 모델에서 테이블을 호출 할 수 있습니다. (필자가 보았던 것처럼 수동으로 weatherConditionTable 을 WeatherConditionModel (클래스!)로 수동 변경했습니다.) 제 3의 foreach 루프에서 할 수 있습니다. 내 데이터를 내 데이터베이스에 저장하십시오. .i 콤보 박스와 목록 상자 및 버튼이 있습니다. 콤보 박스에서 내 도시 을 선택하고 내 GetRss에 명령을 사용하는 버튼을 누르면 데이터가 잘 보입니다. 데이터가 표시됩니다. 데이터베이스에 저장합니다. 내보기 모델 코드입니다 (설명 된 부분) :

 internal void GetRssFeed() 
      { 
      Feed selectedFeed = (Feed)FeedModel.FeedList[FeedModel.SelectedIndex]; 
      FeedModel.SelectedFeedName = selectedFeed.FeedName; 
      WebClient xmlclient = new WebClient(); 
      xmlclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xmlclient_DownloadStringCompleted); 
     xmlclient.DownloadStringAsync(new Uri(selectedFeed.FeedUrl)); 

    } 

    WeatherConditionDomainContext context = new WeatherConditionDomainContext(); 
    WeatherConditionTable wct = new WeatherConditionTable(); 
    void xmlclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     XNamespace yweather = "http://xml.weather.yahoo.com/ns/rss/1.0"; 
     if (e.Error == null) 
     { 

      XElement x = XElement.Parse(e.Result); 
      weatherquery1 = 
       from items in x.Descendants("channel") 
       let item=items.Element("item") 
       select new WeatherConditionModel 
       { 
        Temp = Convert.ToInt32(item.Element(yweather + "condition").Attribute("temp").Value), 
        PubDate = item.Element(yweather + "condition").Attribute("date").Value, 
        Status = item.Element(yweather + "condition").Attribute("text").Value, 
        Humidity=Convert.ToInt32(items.Element(yweather + "atmosphere").Attribute("humidity").Value) 
       }; 

      foreach (WeatherConditionModel wc in weatherquery1) 
      { 

        wct.Temp = wc.Temp; 
        wct.Status = wc.Status; 
        wct.PubDate = DateTime.Now.ToShortTimeString(); 
        wct.Humidity = wc.Humidity; 
        context.WeatherConditionTables.Add(wct); 
        context.SubmitChanges(); 


      } 
     } 
     else 
     { 
      MessageBox.Show(e.Error.Message); 
     } 
    } 

모든 관심을 가져 주셔서 감사합니다. 누군가 도움이 되었으면 좋겠습니다. 더 좋은 아이디어가 있다면 알려주세요. 도움이된다면 답을 표시해주세요.

0

난 당신이 뭘 하려는지 이해하기 힘든 시간을 보내고 있어요,하지만 더 좋아 보여 마지막 단계의 패턴이 있어야한다 : 내가 예약 할 그것을 .Many 덕분에 발견

// Create data context 
WeatherConditionDomainContext context = new WeatherConditionDomainContext(); 

// Load existing entities 
EntityQuery<BusinessApplication1.Web.Models.WeatherConditionModel> weatherLoadQuery = context.GetWeatherConditionDataQuery(); 
context.Load<BusinessApplication1.Web.Models.WeatherConditionModel>(weatherLoadQuery); 

// Update or insert new entries 
foreach (BusinessApplication1.Web.Models.WeatherConditionModel el in weatherquery1) 
{ 
    // Update existing entries 

    // Or, add new entries if they did not exists 
} 

// Submit all changes (updates & inserts) 
context.SubmitChanges(delegate(SubmitOperation operation) 
    { 
     if (operation.HasError) 
     { 
      operation.MarkErrorAsHandled(); 
     } 
    }, null); 
+0

:/업데이트 또는 (weatherquery1에 BusinessApplication1.Web.Models.WeatherConditionModel 엘) 새 항목을 foreach 문을 삽입 { // 업데이트 기존 항목 가되지 존재 않은 경우 // 또는 새 항목을 추가} 방법 내가 그걸 할까? 어떻게 여기에 삽입 할 수 있습니까? – azad6026

+0

데이터 컨텍스트의 해당 테이블에 개체를 추가하여 삽입 작업을 수행합니다. 이것은 Silverlight 전용이 아닌 기본적인 Entity Framework (EF)입니다. EF에서 가장 잘 읽습니다. –

+0

thanks.but 내보기 모델에서 나는 테이블 이름이나 entity.i에 대한 액세스 권한이 없습니다 쓸 수 없습니다 : Mytable t = new Mytable(); T, Column = "something"; ...... context.Mytable. 추가 (t); 컨텍스트, submitchanges; 코드 숨김에서 당신은 할 수 있지만 나는 그것을 mvvm에서 원한다. – azad6026