开发者

WPF: Loading JPG file, saving it into MS SQL DB

I'm new to WPF and programming in general. I set up a dummy db table with one coumn being IMAGE. I now did a WPF window where by clicking a button, a OpenFileDialog appears to load jpg files. When selceting a JPG file and confirming, the image is being displayed in my wp开发者_Go百科f window. Untill here things work for me. Now that the image is loaded and displayed I want to click on another button to save that image into my SQL database. I have no idea how to do that, I suppose I have to convert the image into binary code, or something? Furthermore I don't have a clue how to do the sql query (INSERT INTO tb_test VALUES('Title', MYIMAGEOBJECT?); ??).

also, I should mention, that I already have a connection to the db, excecuting queries is already possible for me.

The code I use so far is below, any hint is appreciated!

 private void openImage()
        {   
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = @_imagepath;
            openFileDialog1.Title = "Browse Image Files";
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;
            openFileDialog1.DefaultExt = "jpg";
            openFileDialog1.Filter = "JPG files (*.jpg)|*.jpg|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;

            Nullable<bool> result = openFileDialog1.ShowDialog();

            if (result==true)
            {
                //display file's path in txt box
                _txtBxArtwork.Text = openFileDialog1.FileName;

                // Convert string to image source
                ImageSourceConverter imgConv = new ImageSourceConverter();
                ImageSource imageSource = (ImageSource)imgConv.ConvertFromString(openFileDialog1.FileName);
                _imagePreview.Source = imageSource;

              // set new image path
                setNewImagePath(System.IO.Path.GetDirectoryName(openFileDialog1.FileName));
            }
        }

        private void setNewImagePath(String newpath)
        {
            _imagepath = newpath;
        }


You should save the image as binary in database. All you have to do is to read the file in byte array, and persist it in database. Look at the sample shown below:

byte[] image = File.ReadAllBytes(@"C:\image.jpg");
using(SqlConnection sc = new SqlConnection())
{
    using(SqlCommand cmd = new SqlCommand(sc, "")
    {
        sc.ConnectionString = connectionString;
        cmd.CommandText = "INSERT INTO TABLE(Title, ImageFile) VALUES(@Title, @Img)";
        cmd.Parameters.Add(new SqlParameter("@Title", "Image title"));
        cmd.Parameters.Add(new SqlParameter("@Img", image));
        sc.Open();
        cmd.ExecuteNonQuery();
    }
}

I don't have VS on this machine, so I was not able to test the code, there may be some syntax errors but hope you will solve that. Anyway if something's wrong, let me know.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜