我该如何解决这一问题 [英] How do I solve this line

查看:102
本文介绍了我该如何解决这一问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从数据库中检索图像数据类型并将图像加载到标签中。



在我的数据库中,图像列是BINARY DATA



我试过这个并显示系统。 byte []



//检索代码

string pt = dr [Photo]。ToString();

image = Encoding.ASCII.GetBytes(pt);

解决方案

这是检索二进制字段并将其转换为图像的一种方法。

 SqlConnection conn = ...; 
SqlCommand cmd = new SqlCommand(SELECT img FROM images WHERE imgId = 42,conn);
SqlDataAdapter dataAdapter = new SqlDataAdapter();
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
if(dataSet.Tables [0] .Rows.Count == 1){
Byte [] data = new Byte [0];
data =(Byte [])(dataSet.Tables [0] .Rows [0] [img]);
using(MemoryStream ms = new MemoryStream(data)){
myLabel.Image = Image.FromStream(ms);
}
}

/ ravi


如果你不明白为什么,那么你的代码的两个位都有可能是错误的: INSERT进入数据库,然后选择SELECT取回图像。



看看这里:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ] - 它解释了你做错了什么,并告诉你如何正确地做到这一点。


使用此功能从数据库中检索图像



创建一个处理程序文件并使用此代码。



 公开 函数 ShowEmpImage( ByVal  ID 作为 整数作为
尝试
con = SqlConnection(DB.GetCon)
Dim sql As String = SELECT top 1 Image FROM tablname WHERE ID = @ID
Dim cmd 作为 SqlCommand = SqlCommand(sql,con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue( @ ID,ID)
con.Open()
Dim img 作为 对象 = cmd.ExecuteScalar()
返回 MemoryStream( CType (img,字节() ))

Catch
返回 没什么
最后
con.Close()
结束 尝试
结束 功能


Hi, how do you retrieve a image datatype from database and load the image to your label.

In my database, the image column is BINARY DATA

I tried this and it shows system.byte[]

//code for retrieving
string pt = dr["Photo"].ToString();
image = Encoding.ASCII.GetBytes(pt);

解决方案

Here's one way to retrieve the binary field and convert it to an image.

SqlConnection conn = ...;
SqlCommand cmd = new SqlCommand ("SELECT img FROM images WHERE imgId = 42", conn);
SqlDataAdapter dataAdapter = new SqlDataAdapter();
DataSet dataSet = new DataSet();
dataAdapter.Fill (dataSet);
if (dataSet.Tables[0].Rows.Count == 1) {
    Byte[] data = new Byte[0];
    data = (Byte[])(dataSet.Tables[0].Rows[0]["img"]);
    using (MemoryStream ms = new MemoryStream (data)) {
      myLabel.Image= Image.FromStream (ms);
    }
}

/ravi


If you don't understand why, then the chances are both bits of your code are wrong: the INSERT into the database and the SELECT to fetch the image back.

Have a look here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it explains what you are doing wrong, and shows you how to do it properly.


Use this function to retrieve image from database

Create a handler file and use this code.

Public Function ShowEmpImage(ByVal ID As Integer) As Stream
        Try
            con = New SqlConnection(DB.GetCon)
            Dim sql As String = "SELECT top 1 Image  FROM tablname WHERE ID= @ID"
            Dim cmd As SqlCommand = New SqlCommand(sql, con)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@ID", ID)
            con.Open()
            Dim img As Object = cmd.ExecuteScalar()
            Return New MemoryStream(CType(img, Byte()))

        Catch
            Return Nothing
        Finally
            con.Close()
        End Try
    End Function


这篇关于我该如何解决这一问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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