2011-04-22 7 views
2

これは私の最初の「本当の」C#プログラムです。指定されたディレクトリを取り、拡張子なしでファイル名を抽出してSQLデータベースに書き込みます。このデータは次に配列に読み込まれ、下の 'foreach'ループに渡されます。ループは、データを使用してIMDBを検索し、最初の結果のURLをDBに格納します。このデータを変数に戻して、これを使ってディレクター、キャスト、プロットなどのページからデータをスクレイプします。SQL UPDATEでデータベースが更新されていません。SQL文に問題がありますか?

私はこのプログラムを監督と最後にデータベースを更新していますキャスト、プロットなどのデータ。私はプログラムを掘り下げて、すべての変数に正しい値が含まれています。フォームがDataGridのテーブルをロードするときに、前にループに追加したデータはすべて表示されますが、ディレクターなどは表示されません。

私は、プログラムの終わりに私のSQLステートメントが間違っていると思う。私は、コードがおそらく非効率で、面倒であることを知っていますが、私はこのすべてに新しいので、簡単に行く!

 foreach (string title in titles) 
     { 
      //Use each title in titles array to search IMDB and return the page URL 
      string searchURL = "http://www.imdb.com/find?s=all&q=" + title; 
      string url = searchURL; 
      string sourceCode = WorkerClass.getSourceCode(url); 
      int startIndex = sourceCode.IndexOf("Media from "); 
      sourceCode = sourceCode.Substring(startIndex, sourceCode.Length - startIndex); 
      startIndex = sourceCode.IndexOf("<a href=") + 9; 
      int endIndex = sourceCode.IndexOf('"' + " onclick", startIndex); 
      string link = "http://www.imdb.com" + (sourceCode.Substring(startIndex, endIndex - startIndex)); 

      //Update DB to add page url based on title 
      SqlConnection con = new SqlConnection(DataAccess.GetConnectionString("dbCon")); 
      //Create SQL Command 
      var command = new SqlCommand("UPDATE movieTable SET [email protected] WHERE [email protected]", con); 
      command.Parameters.AddWithValue("@pageURL", link); 
      command.Parameters.AddWithValue("@title", title); 
      con.Open(); 
      //Add to DB 
      command.ExecuteNonQuery(); 
      con.Close(); 

      //Select IMDB Page URL from movieTable where the title = current title 
      var com = new SqlCommand("SELECT imdbPageURL FROM movieTable WHERE [email protected]", con); 
      con.Open(); 
      com.Parameters.AddWithValue("@title", title); 
      string pageURL = (string)com.ExecuteScalar(); 
      con.Close(); 

      //Get Director 
      sourceCode = WorkerClass.getSourceCode(pageURL); 
      startIndex = sourceCode.IndexOf("description"); 
      sourceCode = sourceCode.Substring(startIndex, sourceCode.Length - startIndex); 
      startIndex = sourceCode.IndexOf("content=") +21; 
      endIndex = sourceCode.IndexOf('.' , startIndex); 
      string director = sourceCode.Substring(startIndex, endIndex - startIndex); 

      //Get Cast 
      sourceCode = WorkerClass.getSourceCode(pageURL); 
      startIndex = sourceCode.IndexOf("content="); 
      sourceCode = sourceCode.Substring(startIndex, sourceCode.Length - startIndex); 
      startIndex = sourceCode.IndexOf('.') +2; 
      endIndex = sourceCode.IndexOf("/>", startIndex) -3; 
      string cast = sourceCode.Substring(startIndex, endIndex - startIndex); 

      //Get Plot 
      sourceCode = WorkerClass.getSourceCode(pageURL); 
      startIndex = sourceCode.IndexOf("Users:"); 
      sourceCode = sourceCode.Substring(startIndex, sourceCode.Length - startIndex); 
      startIndex = sourceCode.IndexOf("</div>"); 
      endIndex = sourceCode.IndexOf("<div", startIndex); 
      sourceCode = sourceCode.Substring(startIndex, endIndex - startIndex); 
      startIndex = sourceCode.IndexOf("<p>") +7; 
      endIndex = sourceCode.IndexOf("</p>"); 
      string plot = sourceCode.Substring(startIndex, endIndex - startIndex); 

      //Get Rating 
      sourceCode = WorkerClass.getSourceCode(pageURL); 
      startIndex = sourceCode.IndexOf("infobar"); 
      sourceCode = sourceCode.Substring(startIndex, sourceCode.Length - startIndex); 
      startIndex = sourceCode.IndexOf("alt=") +5; 
      endIndex = sourceCode.IndexOf("src=", startIndex) -2; 
      string rating = sourceCode.Substring(startIndex, endIndex - startIndex); 

      //Get Release Date 
      sourceCode = WorkerClass.getSourceCode(pageURL); 
      startIndex = sourceCode.IndexOf("infobar"); 
      sourceCode = sourceCode.Substring(startIndex, sourceCode.Length - startIndex); 
      startIndex = sourceCode.IndexOf("nobr"); 
      endIndex = sourceCode.IndexOf("</div>", startIndex); 
      sourceCode = sourceCode.Substring(startIndex, endIndex - startIndex); 
      startIndex = sourceCode.IndexOf("dates") +11; 
      endIndex = sourceCode.IndexOf("</a") -4; 
      string releaseDate = sourceCode.Substring(startIndex, endIndex - startIndex); 

      //Get link to Cover Image 
      sourceCode = WorkerClass.getSourceCode(pageURL); 
      startIndex = sourceCode.IndexOf("img_primary"); 
      sourceCode = sourceCode.Substring(startIndex, sourceCode.Length - startIndex); 
      startIndex = sourceCode.IndexOf("<img src=") + 10; 
      endIndex = sourceCode.IndexOf(".jpg", startIndex) +4; 
      string coverURL = sourceCode.Substring(startIndex, endIndex - startIndex); 

      //Update movieTable with scraped data for the current title 
      var comd = new SqlCommand("UPDATE movieTable SET [email protected], [email protected], [email protected], [email protected], [email protected], [email protected] WHERE [email protected]", con); 
      comd.Parameters.AddWithValue("@title", title); 
      comd.Parameters.AddWithValue("@director", director); 
      comd.Parameters.AddWithValue("@cast", cast); 
      comd.Parameters.AddWithValue("@plot", plot); 
      comd.Parameters.AddWithValue("@rating", rating); 
      comd.Parameters.AddWithValue("@releaseDate", releaseDate); 
      comd.Parameters.AddWithValue("@coverURL", coverURL); 
      con.Open(); 
      //Add to DB 
      command.ExecuteNonQuery(); 
      con.Close(); 
     } 

     this.movieTableTableAdapter.Fill(this.movieLibraryDBDataSet.movieTable); 
+0

'command.ExecuteNonQuery()は何を返していますか? 0か1か何か?また、テーブルの主キーは何ですか? –

+0

Btw、そこにはIDisposableのオブジェクトがたくさんあります。これは、それらが正しく配置されていることを確認するために 'using'ブロックを使う必要があることを意味します。これは、範囲外のオブジェクトの誤った使い方も示しています(回答の1つで示唆されているように) –

+0

SQL接続の目標はできるだけ開いたり閉じたりしないことです。接続を開く前に、すべてのimdbデータを取得し、更新パラメータを解析することができます。 – dotjoe

答えて

3

最終更新では、後でアップデート用に定義するcomdオブジェクトではなく、commandオブジェクトを使用しています。

+0

母、それはそれを並べ替え、ありがとう!私はそれが同様にオブジェクトに名前を付けるために得られるものだと思います! :/ –

1

レコードは既に存在しますか?あなたはINSERTをする必要があると思います。 UPDATEは既存のレコードのみを更新します。