Ado.net图像操作

发布于 2024-10-05 10:45:11 字数 181 浏览 3 评论 0原文

在我的 C# winform 应用程序中。我已成功将程序连接到MS SQL Server 2005,现在我想添加一个Image类型的新列,如何将图像插入DB并把它拿回来?在我们的业务逻辑类中我们将声明哪种数据类型变量?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦爱 2024-10-12 10:45:11

如何将图像插入数据库并取回?

您需要以字节为单位获取图像,如下面的代码

编辑的代码示例

   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();
            }


        }
    }

在我们的业务逻辑类中我们将声明哪种数据类型变量?

您需要将图像属性定义为 Byte[] 并使用上面得到的 bytes[] 分配该属性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文