2011-04-22 50 views
2

这是我第一次尝试'真正的'C#程序。它需要一个指定的目录,提取文件名(没有扩展名)并将它们写入SQL数据库。然后将这些数据读回到数组中并传递到下面的'foreach'循环中。循环然后使用数据搜索IMDB并将第一个结果的URL存储到数据库中。然后它将这些数据读回到一个变量中,并使用它来从页面上'刮'数据,例如导演,演员表,剧情等。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还是什么?另外,您的桌面主键是什么? – 2011-04-22 16:31:07

+0

顺便说一句,你有很多对象是IDisposable;这意味着您需要使用'using'块来确保它们正确放置。这也将显示任何不正确使用超出范围的对象,如其中一个答案中所暗示 – 2011-04-22 16:40:44

+0

sql连接的目标是尽可能少地打开和关闭它。在打开连接之前,您可以获取所有imdb数据并解析更新参数。 – dotjoe 2011-04-22 17:18:46

回答

3

上一次执行的是使用command对象而不是稍后为更新定义的comd对象。

+0

哈哈,这是排序它,谢谢!我想这就是你得到类似的命名对象! :/ – 2011-04-22 16:41:26

1

记录是否已经存在?我认为你需要做一个INSERT。更新只更新现有的记录。