如何将图片存储在图像列中? [英] How do you store a picture in an image column?

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

问题描述

我有一个用户表:

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);

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

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