数据集不会将图像更新到数据库

时间:2013-12-22 09:32:51

标签: c# sql sql-server dataset

我正在尝试将图像插入到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方法时不会更新对实际数据集的任何更改,而不仅仅是图像,但是如果我在调试时使用数据可视化工具更改了表数据。

1 个答案:

答案 0 :(得分:0)

然而,我又在一天后回答了我自己的问题,希望我得到更好的回答,但我想我的信息不一定是最好的。

我需要在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();

}