将图像存储在SQL Server中 [英] Store images in sql server

查看:90
本文介绍了将图像存储在SQL Server中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在开发一个项目,以从生物特征识别设备捕获指纹图像并将其存储在数据库中.我的问题是将图像直接存储到数据库中或将其存储在文件中以及将文件的路径存储到数据库中是否好".请将代码发送给这两种情况.

解决方案

如果将图像存储在文件夹中并且仅将图像的路径存储在sql server数据库中,则很有用. blockquote>

嗨 您应该在数据库中定义字段varbinary(MAX)
然后使用此代码

FileStream file = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
            BinaryReader str1 = new BinaryReader(file);
            byte[] bit = str1.ReadBytes(Convert.ToInt32( file.Length));
            SqlConnection connection = new SqlConnection("server=(local);database=test;integrated security=true");
            connection.Open();
            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandType = CommandType.Text;
            command.Parameters.AddWithValue("@Photo",bit);
            command.CommandText = "INSERT INTO image1(pic) VALUES (@Photo)";
            command.ExecuteNonQuery();
            connection.Close();


理想情况下,您不应该将整个图像存储到数据库中.仅存储该图像的路径.


Am developing a project to capture an finger print image from the biometric device and store it in database. My question is "Is it good to store the image directly into the database or store it in file and store the path of the file into the database." Please send the code to both cases.

解决方案

It is useful if you store the images in a folder and store only path of that images in sql server database.


hi you should define a field varbinary(MAX) in database
then use this code

FileStream file = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
            BinaryReader str1 = new BinaryReader(file);
            byte[] bit = str1.ReadBytes(Convert.ToInt32( file.Length));
            SqlConnection connection = new SqlConnection("server=(local);database=test;integrated security=true");
            connection.Open();
            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandType = CommandType.Text;
            command.Parameters.AddWithValue("@Photo",bit);
            command.CommandText = "INSERT INTO image1(pic) VALUES (@Photo)";
            command.ExecuteNonQuery();
            connection.Close();


Ideally you should not store the entire image into database. Store only path of that image.


这篇关于将图像存储在SQL Server中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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