如何将图片存储在图像列中?
问题描述:
我有一个用户表:
Name varchar(20)
Picture image
我想将图像存储到图片"列中——我如何使用 SQL 脚本来实现这一点?
I want to store a image into the Picture column -- how can I achieve this using SQL Scripts?
答
这里是将图片存储到 sql server 的示例代码:
Here is a sample code for storing image to sql server :
SqlConnection conn = new SqlConnection(connectionString);
try
{
int imageLength = uploadInput.PostedFile.ContentLength;
byte[] picbyte = new byte[imageLength];
uploadInput.PostedFile.InputStream.Read (picbyte, 0, imageLength);
SqlCommand command = new SqlCommand("INSERT INTO ImageTable (ImageFile) VALUES (@Image)", conn);
command.Parameters.Add("@Image", SqlDbType.Image);
command.Parameters[0].Value = picbyte;
conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
注意: uploadInput 是一个文件输入控件,用于上传图片文件到服务器.取自 ASP.NET 应用程序的代码.
NOTE : uploadInput is a file input control, to upload image file to server. The code taken from an ASP.NET application.
这是插入图像类型列的脚本:
EDIT : Here is the insert script to an image typed column :
INSERT INTO ImageTable (ImageColumn)
SELECT ImageColumn FROM
OPENROWSET(BULK N'C:\SampleImage.jpg', SINGLE_BLOB)
AS ImageSource(ImageColumn);