如何在SQL数据库中存储文件系统中的映像及其路径 [英] How to store Images in file system and their path in SQL Database

查看:110
本文介绍了如何在SQL数据库中存储文件系统中的映像及其路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





我正在学校管理项目。

我需要在该配置文件中添加Faculties和Student照片。



我想在文件系统中添加图像文件,在SQL数据库中添加路径为nvarchar(255)。



请描述一下,如何实现这个目标?

Hi to all,


I am working on a School Management Project.
I need to add Faculties and Students photos in there profile.

I want to add image files in file system and there paths in SQL Database as nvarchar(255).

Please describe me, how to achieve this?

推荐答案

如果你想保存照片的路径数据库,我建议你使用FileUpload对象。

通过这种方式你可以得到文件,并可以用这种方式保存一些表。

示例(在.Net):
If you want to save the path of the photos in the database, I suggest you use the FileUpload object.
In this way you get the File and may save some table in this way.
Example (In. Net):
FileUpload.PostedFile.FileName.ToString.Trim()


注意:您希望将图像存储在哪一列数据库,应声明类型 - 图像



Note: The column,where u want to store ur images in database,should be declared type-Image

byte[] Attachmnt;
 private void btnBrowse_Click(object sender, EventArgs e) //Button by which u will select image from ur system
{   
            
OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Title = "Select image Files Only";

openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;

openFileDialog1.Filter = "Jpeg files (*.jpg)|*.jpg|All Files(*.*)|*.*"; 
//Specify format above as u want
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;

openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;
               
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
tbUpload.Text = openFileDialog1.SafeFileName;//Here u get Only fileName
FilePath = openFileDialog1.FileName;//Here u will get FilePath
if (openFileDialog1.FileName != null)  
{//this block will convert ur image file to byte array format, which will be accept by ur database column''s where type is "image" u must declare
System.IO.FileStream fs = new System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fs);
long byteLength = new System.IO.FileInfo(FilePath).Length;
Attachmnt = binaryReader.ReadBytes((Int32)byteLength);
//Above in Attachmnt u will get ur image file to byte format now u can save the value of "Attachmnt" to that column,either by stored procedure or by query
//Now at next step,is u want to get it from ur database to ur system or anywhere, u have to again convert into byte array format.
fs.Close();
fs.Dispose();
binaryReader.Close();
                    }                    
                }
               
             }      


这篇关于如何在SQL数据库中存储文件系统中的映像及其路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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