如何从opendialog框中选择的文件中将图像保存在数据库中 [英] how to save image in database from the file selected from opendialog box

查看:88
本文介绍了如何从opendialog框中选择的文件中将图像保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列名称为Image和数据类型为image的表,当我选择文件名保存到数据库时,它说该字符串与数据类型不匹配.所选文件的路径类似于C:\\ Users \\ mydocument \\ pic之类的东西......所以我该如何保存文件,请提供代码
bishnu karki

i have a table with column name Image and datatype image, when i choose filename to save into database, its says that the string doesnot match the datatype. the path of the selected file is like C:\\Users\\mydocument\\pic something like that......so how can i save file , please provide with the code
bishnu karki

推荐答案

Thta是因为图像用于存储大量二进制数据:图像数据本身.
如果要存储文件名,则将其转换为字节数组:
Thta is because image is for storing a large quantity of binary data: the Image data itself.
If you want to store filename, then either convert it to a byte array:
string s = " C:\\Users\\mydocument\\pic";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);


或将数据库中的数据类型更改为字符串类型:例如NVARCHAR.


or change the datatype in your database to a string type: NVARCHAR for example.


与其将图像本身存储在数据库中,不如将其存储在数据库中,而是尝试仅存储图像的路径,然后在需要时要在您的应用程序中使用该映像,您只需检索路径并从磁盘中加载它即可.这是一种非常简单的方法,无需额外的转换和操作即可处理.
Rather than storing the image itself in the database, try storing just the path of the image and then when you want to use the image in your app, you can just retrieve the path and load it from the your disk. Its a much simpler approach with no extra conversions and manipulations to take care of.


这篇关于如何从opendialog框中选择的文件中将图像保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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