在ASP.NET中将图像保存到SQL Server数据库的最佳步骤是什么? [英] what is the best procedure to save images into SQL Server database in ASP.NET?

查看:52
本文介绍了在ASP.NET中将图像保存到SQL Server数据库的最佳步骤是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。在我的项目中,我需要将用户上传的图像保存到sql Server数据库中。我想通过拍摄图像类型列将这些图像直接存储到数据库中,我会在需要时检索它们。但是我的一些朋友说这会导致因为应用程序需要直接从数据库下载这些图像,因此,他们建议只保存数据库中的图像路径。但是这里我的问题是如果存储的路径中没有任何图像可能会出现这种情况或遗失任何机会?。我在这种情况下完全感到困惑。所以,请各位指导我根据这种情况我可以实施安全的方法,我可以同时避免表现



先谢谢

Hi to all. In my project i need to save user uploaded images into sql Server database.I thought to store those images directly into database by taking Image type column and i will retrieve them whenever i need to.But some of my friends are saying that this will result into performance issues as the app need to download those images directly from database.So,they are suggesting to save only the paths of images in database.but here my problem is what will be the situation if any image is not available in the stored path or missing by any chance?.I am totally confused in this scenario.So, guys please guide me according to this scenario where i can implement secured approach and i can avoid performance at the same time

Thanks in Advance

推荐答案

http://www.aspdotnet-suresh.com/2011/01/how-to-insert-im age-into-database-and.html [ ^ ]


这里需要根据需求来决定。

是否总计预期图像的大小很容易存储在数据库中。将数据存储在数据库中将会增加对数据库的负担。



如果数据库服务器在数据库中保存,则在数据库中存储图像的路径会更好。将文件存储在文件系统中也会提供更大的灵活性和可扩展性。



关于如果在文件系统中保存图像的安全性,我认为它将存储在一些安全的服务器上这些都不是所有人都可以获得的。因此,如果您的服务器足够安全,那么我没有看到任何安全问题。



因此,请根据您的图像大小和数据库服务器功能来决定。
Here you need to decide based on requirement.
Whether the total size of expected images would easlily be stored on database. Storing image in DB would obiously will increase load on DB.

Storing the path of image in database is better in case your database server degrades if saved in DB. Also storing image in file system will provide much more flexibility and scalability.

Regarding securty if save images in file system, I think it will be stored on some secured server and these will not be accessible to all. So if your server is secured enough then I don''t see any security issue.

So decide based on your size of the images and database server capability.


// CREATE DATABASE CONNECTION
        using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString))
        {
            // OPEN CONNECTION
            con.Open();

            // READ IMAGE BYTES
            byte[] bt = System.IO.File.ReadAllBytes(@"D:\image.jpg");

            // CREATE SQLCOMMAND AND SPECIFY PROPERTIES
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();

            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "your_storedprocedure_name";

            // ASSIGN IMAGE TO StoredProcedure PARAMETER
            System.Data.SqlClient.SqlParameter param = new System.Data.SqlClient.SqlParameter("@imgdata", bt);
            cmd.Parameters.Add(param);

            // EXECUTE StoredProcedure TO SAVE IMAGE
            cmd.ExecuteNonQuery();

            // CLOSE CONNECTION
            con.Close();
        }


这篇关于在ASP.NET中将图像保存到SQL Server数据库的最佳步骤是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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