简体   繁体   English

如何在 Visual Studio 2010 的数据库中包含图像

[英]How do I include Images in my Database in Visual Studio 2010

I'm fairly new to C# .Net.我对 C# .Net 相当陌生。 We're being taught it at University and are using Visual Studio to create windows forms.我们在大学学习它,并且正在使用 Visual Studio 创建 Windows 窗体。 As a new part to the subject we're using databases, tables and datasets.作为该主题的新部分,我们正在使用数据库、表格和数据集。

I opened a new Windows Form project and immediately added a new database to it.我打开了一个新的 Windows 窗体项目,并立即向其中添加了一个新数据库。 The table I want to create will have 2 columns - ImageID and the image itself.我要创建的表将有 2 列 - ImageID 和图像本身。 In what way do i add the image in to the box?我以什么方式将图像添加到框中? I've tried full path, relative path and dragging the image in, but whatever I do i get the same error message....我已经尝试过完整路径、相对路径和拖动图像,但无论我做什么,我都会收到相同的错误消息....

Invalid Value无效值
The changed value in this cell was not recognized as being valid.此单元格中更改的值未被识别为有效。 .Net Framework Data Type: Byte[] .Net 框架数据类型:字节 []
Error Message: You cannot use the result pane to set this Field data to values other than NULL错误消息:您不能使用结果窗格将此字段数据设置为 NULL 以外的值
Type a value appropriate for the data type or press ESC to cancel the change键入适合数据类型的值或按 ESC 取消更改

How can I have images in there?我怎样才能在里面放图像? I just don't know how to use the image data type within the table.我只是不知道如何在表中使用图像数据类型。 Any help is much appreciated.任何帮助深表感谢。

A simpler approach is to store the image in the file system and only its path in the database.一种更简单的方法是将图像存储在文件系统中,并且仅将其路径存储在数据库中。 Basically you define a base folder:基本上你定义一个基本文件夹:

string baseFolder = "c:\Program Files\MyApp\Images";

And use it to store relative paths in the database:并用它来存储数据库中的相对路径:

INSERT INTO ImagesTable (Name, Path)
Values ('German Shepherd', 'Dogs\german-shepherd.jpg')

Then, when you need to retrieve the image, you can do it like this:然后,当您需要检索图像时,您可以这样做:

string path = Path.Combine(baseFolder, 'Dogs\german-shepherd.jpg');
Image img = Image.FromFile(path);

In the following SO question you can find more information about the pros and cons of this approach:在以下 SO 问题中,您可以找到有关此方法优缺点的更多信息:

you can store images in sql server 2008. Just create database table having column datatype "image".您可以将图像存储在 sql server 2008 中。只需创建具有列数据类型“图像”的数据库表。 now from .net code use the file upload control to select the image file and then convert the image parameter into byte[] before inserting image data into the database.现在从.net代码中使用文件上传控件选择图像文件,然后将图像参数转换为字节[],然后将图像数据插入数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更新数据库源后,如何让 Visual Studio 2010 刷新我的数据集? - How do I get Visual Studio 2010 to refresh my DataSet after I have updated the Database source? 如何将Microsoft Visual C ++ 2010 SP1与我的Visual Studio安装程序项目一起打包 - How do I package Microsoft Visual C++ 2010 SP1 with my Visual Studio installer project 如何在我的设置中包含Visual C ++ 2010可再分发? - How do I include Visual C++ 2010 redistribuable in my setup? 如何在Visual Studio 2010中使用我的设置和部署项目部署SQL Server数据库? - How can I deploy the SQL server database with my setup and deployment project in Visual Studio 2010? 我的数据库在Visual Studio 2010中在哪里? - Where is my database in Visual Studio 2010? 如何为我的 Visual Studio 2010 C# 项目制作安装程序? - How do I make an installer for my Visual Studio 2010 C# project? 如何在Visual Studio 2010中停止自动轮廓扩展? - How do I stop Auto Outline Expansion in Visual Studio 2010? 在Visual Studio 2010中进行调试时如何设置IIS? - How do I setup IIS when debugging in Visual Studio 2010? 如何在Visual Studio 2010 C#中显示ApplicationRevision - How do I show the ApplicationRevision in Visual Studio 2010 C# 如何在Visual Studio 2010中有条件地执行一行? - How do I execute a line conditionally in Visual Studio 2010?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM