如何在SQL Server表中添加图像 [英] how to add image in sql server table

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

问题描述

我想知道如何在MS SQL Server中将图像添加到表中.

解决方案

1)调出SQL Server Management Studio.
2)连接,然后打开要将图像添加到的数据库.
3)展开表,然后添加带有适当索引字段的新表,或者右键单击该表并选择设计.
4)添加一个名为"myImage"的字段,并将其数据类型设置为"image".允许为空.
5)关闭SQL Server Management Studio.

要将图像添加到表中(我将假设一个新记录,该表具有一个Identity字段,而只是myImage列):

using (SqlConnection con = new SqlConnection(connectionString))
    {
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myImage) VALUES (@IM)", con))
        {
        byte[] imageData = File.ReadAllBytes(@"F:\Temp\Picture.jpg");
        com.Parameters.AddWithValue("@IM", imageData);
        com.ExecuteNonQuery();
        }
    }


请参见使用Microsoft .NET从SQL Server存储和检索图像 [ ^ ].

您可以创建数据类型为"image"的字段.这将在您的编程代码中用byte[]表示.

因此,如果您有图像,请使用流将其打开并将其转换为byte[].然后将其添加到您的SQL表中.

但是,如果您要添加很多图像,建议您将位置(例如c:\ path \ to \ image.jpg)存储在数据库中,因为图像本身可能存储在数据库中大大降低数据库的速度.

玩得开心!
爱德华


I want to know how can I add image to table in MS SQL Server

解决方案

1) Bring up SQL Server Management Studio.
2) Connect, and open the database you want to add the image to.
3) Expand the tables, and either add a new table with an appropriate index field, or right click teh table and select design.
4) Add a field called "myImage", and make its datatype "image". Allow nulls.
5) Close SQL Server Management studio.

To add images to the table (I will assume a new record, the table has an Identity field, and just teh myImage column):

using (SqlConnection con = new SqlConnection(connectionString))
    {
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myImage) VALUES (@IM)", con))
        {
        byte[] imageData = File.ReadAllBytes(@"F:\Temp\Picture.jpg");
        com.Parameters.AddWithValue("@IM", imageData);
        com.ExecuteNonQuery();
        }
    }


See Storing and Retrieving Images from SQL Server using Microsoft .NET[^].


You can create a field of the datatype ''image''. That will be represented by a byte[] in your programming code.

So if you have an image, open it with a stream and convert it to a byte[]. Then add that to your SQL Table.

If you''re going to add a lot of images however, I''d recommend to store the location (e.g. c:\path\to\image.jpg) in your database instead, because storing the image itself in the database may slow your DB down dramaticly.

Have fun!
Eduard


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

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