簡體   English   中英

數據集不會將圖像更新到數據庫

[英]Dataset won't update image to database

我正在嘗試將圖像插入SQL Server數據庫,它進入數據集,但是當調用save方法時,SQL Server不會更改。

public void AddImage(OpenFileDialog openFileDialog1, List<Movie> movieList)
{
    byte[] movieCover = null;
    FileStream movieStream = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
    BinaryReader movieReader = new BinaryReader(movieStream);
    movieCover = movieReader.ReadBytes((int)movieStream.Length);
    var Starwars = new object[2];
    Starwars[0] = "Star Wars: Episode I - The Phantom Menace";
    Starwars[1] = "1999";
    var found = _movieSet.Tables["Movie"].Rows.Find(Starwars);

    if (found != null)
    {
        found.SetField("Cover", movieCover);
        var movieListFound = movieList.Find(x => x.Name == Starwars[0]);
    }
    else
        MessageBox.Show("Movie Not Found");
}

保存方法

 public void Save()
 {
     var movieConnection = new SqlConnection();
     try
     {
         movieConnection = new SqlConnection(Properties.Settings.Default.moviesConnectionString);
         movieConnection.Open();
         _movieAdapter.Update(_movieSet, "Movie");
         movieConnection.Close();                
      }
      catch (Exception e)
      {
         MessageBox.Show(e.Message);
      }
      finally
      {
          movieConnection.Dispose();
      }
 }

添加新行是可行的,但是調用save方法時,對實際數據集的任何形式的更改都不會更新,不僅是圖像,而且如果我在調試中使用數據可視化器更改表數據也不會更新。

一天后,我再次回答自己的問題,希望得到更好的答復,但我想我的信息一定不是最好的。

我需要在Addimage方法期間調用Save()方法。

public void AddImage(OpenFileDialog openFileDialog1, List<Movie> movieList)
{
     byte[] movieCover = null;
     FileStream movieStream = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
     BinaryReader movieReader = new BinaryReader(movieStream);
     movieCover = movieReader.ReadBytes((int)movieStream.Length);
     var Starwars = new object[2];
     Starwars[0] = "Star Wars: Episode I - The Phantom Menace";
     Starwars[1] = 1999;
     var found = MovieTable().Rows.Find(Starwars);
     if (found != null)
     {
         found["Cover"] = movieCover;
         var movieListFound = movieList.Find(x => x.Name == Starwars[0]);
     }
     else
         MessageBox.Show("Movie Not Found");
     Save();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM