[英]How to retrieve image from Oracle database using C# web application?
[英]To retrieve image from oracle database by using imagepath in c#
这是代码:
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 []”的对象。
谁能帮忙吗?
您需要读取文件的内容而不是字节数组的路径:
byte[] file = File.ReadAllBytes(reader["imgpath"].ToString());
根据您的字段名称,您似乎已经在imgpath字段中存储了文件的路径,现在您正在尝试将其转换为byte []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.