简体   繁体   English

从SQL Server VarBinary(Max)读取图像

[英]Read Image from SQL Server VarBinary(Max)

I have a SQL Server Database. 我有一个SQL Server数据库。 I have stored an image in there which is a varbinary(max). 我已经在其中存储了一个varbinary(max)图像。 I inserted the image the following way : 我通过以下方式插入图像:

string consString = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
            SqlConnection con = new SqlConnection(consString);
            con.Open();

            String filePath = fuImage.PostedFile.FileName;
            String naam = Path.GetFileName(filePath);
            String extension = Path.GetExtension(naam);

            Stream stream = fuImage.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(stream);
            Byte[] imgByte = br.ReadBytes((Int32)stream.Length);


            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "spAddImage";
            cmd.Parameters.AddWithValue("@FOTO", imgByte);
            cmd.Parameters.AddWithValue("@ARTIEST", ddlArtiest.SelectedValue);
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            con.Close();

The image now looks like this in the database : It is stored as a varbinary. 现在,图像在数据库中看起来像这样:它存储为varbinary。

http://puu.sh/ikF83/6a03b52520.png <--- database http://puu.sh/ikF83/6a03b52520.png <---数据库

Now I want to display my image on my asp.net page that I have inserted. 现在,我想在我插入的asp.net页面上显示我的图像。

But I have no idea how I can achieve this. 但是我不知道如何实现这一目标。

You could use SqlDataReader to go over the results and get back what you need 您可以使用SqlDataReader遍历结果并取回所需的内容

var reader = cmd.ExecuteReader();
while (reader.Read())
{
  byte[] myImage = (byte[])reader["MyImageColumn"];
}

This is from top of my head so make your adjustments as required. 这是从我的头上来的,因此请根据需要进行调整。 But it should give you the basic idea. 但这应该给您基本的想法。

Furthermore, It's strongly recommended to use the using block as it ensures the object is disposed correctly. 此外,强烈建议使用using块,因为它可以确保正确放置对象。

So you could change your code to 因此,您可以将代码更改为

using(var connection = new SqlConnection(connectionString))
{
  //Your command 
  connection.Open();

  //Datareader here

}//Object disposed here

Reference to SqlDataReader SqlDataReader的引用

Reference to using block 引用使用块

Extending the answer provided by Izzy, below is how we have implemented the same. 扩展Izzy提供的答案,以下是我们如何实现相同的方法。

public HttpResponseMessage RenderImage(RenderRequest renderRequest, HttpRequestMessage request)
{
    var httpResponse = new HttpResponseMessage();

    try
    {
        var reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            byte[] imagebytes = (byte[])reader["MyImageColumn"];
        }

        return httpResponse = request.CreateResponse<byte[]>(HttpStatusCode.OK, imagebytes, new ImageMediaFormatter("image/png"));
    }
    catch ()
    {
        throw;
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM