使用c#中的imagepath从oracle数据库中检索图像

时间:2014-01-07 10:31:04

标签: c# asp.net image oracle handler

以下是代码:

         MemoryStream memoryStream = new MemoryStream();

        string sql = "SELECT imgpath FROM imgemp WHERE empl_code = " + id + "";

        OracleCommand cmd = new OracleCommand(sql, connection);
        //cmd.Parameters.AddWithValue("@id", id);
        connection.Open();

        OracleDataReader reader = cmd.ExecuteReader();
        reader.Read();
        if (reader.HasRows)
        {
            //byte[] file = Encoding.ASCII.GetBytes("imgpath");
            //Get Image Data
            byte[] file = (byte[])reader["imgpath"];

            reader.Close();
            connection.Close();
            memoryStream.Write(file, 0, file.Length);
            context.Response.Buffer = true;
            context.Response.BinaryWrite(file);
            memoryStream.Dispose();

当我运行它时,我收到以下错误,

无法将'System.String'类型的对象强制转换为'System.Byte []'。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

你需要读取文件的内容而不是路径到字节数组:

byte[] file = File.ReadAllBytes(reader["imgpath"].ToString());

答案 1 :(得分:0)

根据您的字段名称,您似乎已在imgpath字段中存储了文件路径,现在您正尝试将其转换为byte []