开发者

Ado.net image operation

In my C# winform app. I connected my program to the MS SQL Server 2005 successfully, now I want to add a new column of type Image, how can I Insert the Image in the DB and get it back? and开发者_开发问答 in our Business Logic Class which data type variable we will declare?


how can I Insert the Image in the DB and get it back?

you need to get the image in bytes as shown in below code

Edited Code Example

   private void BrowseImage(object o)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();

        // Set filter for file extension and default file extension 
        //openFileDialog.DefaultExt = ".bmp";
        //openFileDialog.Filter = "24-Bit Bitmap (.bmp)|*.bmp";
        openFileDialog.InitialDirectory = @"C://"
        openFileDialog.DefaultExt = ".jpg";
        openFileDialog.Filter =
            "BMP (*.BMP)|*.BMP|" +
            "JPEG (*.JPG; *.JPEG; *.JPE)|*.JPG;*JPEG|" +
            "GIF (*.GIF)|*.GIF|" +
            "TIFF (*.TIFF)|*.TIFF|" +
            "PNG (*.PNG)|*.PNG|" +
            "DIB (*.DIB)|*.DIB|" +
            "JFIF (*.JFIF)|*.JFIF";

        // Display OpenFileDialog by calling ShowDialog method 
        Nullable<bool> result = openFileDialog.ShowDialog();

        // Get the selected file name and display in a TextBox 
        if (result == true)
        {
            // Open document 
            string filename = openFileDialog.FileName;


            Stream stream = openFileDialog.OpenFile();
            byte[] bytes = null;
            if (stream != null && stream.CanRead)
            {

                 bytes = new byte[stream.Length];
                 stream.Read(bytes, 0, Convert.ToInt32(stream.Length));
                 stream.Close();
            }


        }
    }

in our Business Logic Class which data type variable we will declare?

you need to define the image property as Byte[] and assign this property with the bytes[] got above.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜