如何在sql数据库中存储和检索图像 [英] How to store and Retrieved image in sql database

查看:129
本文介绍了如何在sql数据库中存储和检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在sql数据库中存储和检索图像

How to store and Retrieved image in sql database

推荐答案

根据图像的大小,我不建议直接将其存储在数据库中 - 小缩略图大小还可以,但是这些天的图像变得非常快,并且可能占用大量的SQL服务器带宽和数据库空间。如果你正在考虑存储大量的大图像,我会为它们创建一个文件夹,并将它们存储在一个临时名称下,然后在数据库中记录它。就个人而言,我将拇指存放在数据库中,并提供完整图像的路径参考。



但是,存储或检索图像并不困难:

Depending on the size of the image, I wouldn't recommend storing it in the DB directly - small thumbnail sizes are ok, but images these days get big pretty fast, and can take up a lot of SQL server bandwidth and DB space. If you are looking at storing a lot of largish images, I would create a folder for them, and store them under a temporary name, recording that in the DB instead. Personally, I store a thumb in the DB, with a path reference to the full image.

However, it isn't difficult to store or retrieve images:
using (SqlConnection con = new SqlConnection(GenericData.DBConnection))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO Images (Id, Location, ImageData, InsertDate) VALUES (@ID, @DS, @TN, @DI)", con))
        {
        cmd.Parameters.AddWithValue("@ID", Id);
        cmd.Parameters.AddWithValue("@DS", Location);
        cmd.Parameters.AddWithValue("@TN", myImage.ToByteArray());
        cmd.Parameters.AddWithValue("@DI", InsertDate);
        cmd.ExecuteNonQuery();
        }
    }




using (SqlConnection con = new SqlConnection(GenericData.DBConnection))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT ImageData FROM Images WHERE Id=@ID", con))
        {
        cmd.Parameters.AddWithValue("@ID", Id);
        SqlDataReader r = cmd.ExecuteReader();
        if (r.Read())
            {
            MemoryStream ms = new MemoryStream((byte[])r["ImageData"]);
            MyImage = Image.FromStream(ms);
            }
        }
    }


参考:

示例程序演示如何在SQL Server中保存或存储图像

存储或在SQL Server中保存图像 [ ^ ]



本文介绍如何使用C#在Microsoft .NET中存储和检索数据库中的图像。

使用Microsoft .NET从SQL Server存储和检索图像 [ ^ ]



您也可以尝试搜索 google [ ^ ]或 CodeProject [< a href =http://www.codeproject.com/search.aspx?q=store+%26+retrieve+image+in+sql&doctypeid=4%3b5target =_ blanktitle =New Window> ^ ]
Refer:
A sample program to demonstrate how to save or store images in SQL server
Store or Save images in SQL Server[^]

This article is about storing and retrieving images from database in Microsoft .NET using C#.
Storing and Retrieving Images from SQL Server using Microsoft .NET[^]

You can also try search on google[^] or CodeProject[^]


使用'BLOB'概念来存储和检索图像。



一个很好的例子是< a href =http://www.akadia.com/services/dotnet_read_write_blob.html> dotnet_read_write_blob_Example 。



查看链接,你可能会能够找到您的查询的解决方案。
use 'BLOB' concept to store and retrieve Images.

A very good example is dotnet_read_write_blob_Example .

go through the link and you might be able to find out solution for your query.


这篇关于如何在sql数据库中存储和检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆