从 Oracle DB 中检索图像
Posted
技术标签:
【中文标题】从 Oracle DB 中检索图像【英文标题】:Retrieve image from Oracle DB 【发布时间】:2016-12-26 23:44:21 【问题描述】:所以,我正在使用 web api 来检索图像!但是,在数据库中,图像是 LongRaw。我在谷歌看到我需要使用 OracleDbType.Blob 但是,当我尝试使用它时,
public IEnumerable<FotoEnvolvido> GetFoto(string suspid)
DataSet lretorno = new DataSet();
string connectionString = GetConnectionString();
using (OracleConnection connection = new OracleConnection())
connection.ConnectionString = connectionString;
OracleDataReader reader = null;
OracleCommand cmd = new OracleCommand();
cmd.InitialLONGFetchSize = 50000;
cmd.Connection = connection;
cmd = new OracleCommand("MOBILE.XAPIMANDADOMOBILE.BUSCAFOTO", connection);
cmd.CommandType = CommandType.StoredProcedure;
//variáveis entrada
cmd.Parameters.Add(new OracleParameter("SUSPID", suspid));
//variáveis de saida
cmd.Parameters.Add(new OracleParameter("oretorno", OracleDbType.Blob)).Direction = ParameterDirection.Output;
connection.Open();
cmd.ExecuteNonQuery();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(lretorno);
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
//CRIO A LISTA
lretorno.Load(reader, LoadOption.OverwriteChanges, "BUSCAFOTO");
connection.Close();
connection.Dispose();
var teste = lretorno.Tables[0].AsEnumerable().Select(row => new FotoEnvolvido
FOTO = (byte[])(row["FOTO"]),
);
return teste;
我在 cmd.ExecuteNonQuery() 上有一个错误:
"ORA-06550: linha 1, coluna 7:\nPLS-00306: número errado ou tipos de argumentos na chamada para 'BUSCAFOTO'\nORA-06550: linha 1, coluna 7:\nPL/SQL: Statement ignored"
Oracle.ManagedDataAccess.Client.OracleException was unhandled by user code
DataSource=""
ErrorCode=-2147467259
HResult=-2147467259
IsRecoverable=false
Message=ORA-06550: linha 1, coluna 7:
PLS-00306: número errado ou tipos de argumentos na chamada para 'BUSCAFOTO'
ORA-06550: linha 1, coluna 7:
PL/SQL: Statement ignored
Number=6550
Procedure=""
Source=Oracle Data Provider for .NET, Managed Driver
StackTrace:
em OracleInternal.ServiceObjects.OracleCommandImpl.VerifyExecution(OracleConnectionImpl connectionImpl, Int32& cursorId, Boolean bThrowArrayBindRelatedErrors, OracleException& exceptionForArrayBindDML, Boolean& hasMoreRowsInDB, Boolean bFirstIterationDone)
em OracleInternal.ServiceObjects.OracleCommandImpl.VerifyExecution(OracleConnectionImpl connectionImpl, Int32& cursorId, Boolean bThrowArrayBindRelatedErrors, OracleException& exceptionForArrayBindDML, Boolean bFirstIterationDone)
em OracleInternal.ServiceObjects.OracleCommandImpl.ExecuteNonQuery(String commandText, OracleParameterCollection paramColl, CommandType commandType, OracleConnectionImpl connectionImpl, Int32 longFetchSize, Int64 clientInitialLOBFS, OracleDependencyImpl orclDependencyImpl, Int64[]& scnFromExecution, OracleParameterCollection& bindByPositionParamColl, Boolean& bBindParamPresent, OracleException& exceptionForArrayBindDML, Boolean isFromEF)
em Oracle.ManagedDataAccess.Client.OracleCommand.ExecuteNonQuery()
em WebApiApp.Controllers.FotoController.GetFoto(String suspid) na C:\Users\50216740\Documents\Visual Studio 2015\Projects\AppMobilePCRJ\AppMobilePCRJ\WebApiApp\Controllers\FotoController.cs:linha 49
em lambda_method(Closure , Object , Object[] )
em System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
em System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
em System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
InnerException:
如果我使用 OracleDbType.RefCursor 像我获取字符串的其他 web api 一样,不要给我这个错误...问题是参数来自 ["ISUSPID":0,"FOTO" :""],即使在 DB 上给我带来了图像!
我做错了什么,那是 Json 上的 FOTO 是空的???
【问题讨论】:
【参考方案1】:我不确定 lretorno.Load(...) 正在做什么来读取数据,但是这个使用 select 语句的 sudo 代码示例可能会对您有所帮助...我一直必须专门获取 blob 和读出它以获取过去的字节。
检索LONG RAW数据类型
的示例var imgCmd = new OracleCommand("SELECT photo FROM photos WHERE photo_id = 1", _con);
imgCmd.InitialLONGFetchSize = -1; // Retrieve the entire image during select instead of possible two round trips to DB
var reader = imgCmd.ExecuteReader();
if (reader.Read())
// Fetch the LONG RAW
OracleBinary imgBinary = reader.GetOracleBinary(0);
// Get the bytes from the binary obj
byte[] imgBytes = imgBinary.IsNull ? null : imgBinary.Value;
reader.Close();
检索 BLOB 数据类型
的示例var imgCmd = new OracleCommand("SELECT photo FROM photos WHERE photo_id = 1", _con);
var reader = imgCmd.ExecuteReader();
if (reader.Read())
// Fetch the blob
OracleBlob imgBlob = reader.GetOracleBlob(0);
// Create byte array to read the blob into
byte[] imgBytes = new byte[imgBlob.Length];
// Read the blob into the byte array
imgBlob.Read(imgBytes, 0, imgBlob.Length);
reader.Close();
【讨论】:
抱歉耽搁了!!并感谢您的回答...但是参数“blob”是谁?你用过 blob.Lenght 正如您从读者那里看到的那样,您获得了 oracle blob 对象,它可以告诉您 blob 的长度,然后该对象还具有读取函数来填充字节数组 在 imgBlob.Read 中给我 imgBlob.Length 上的错误“无法将 long 转换为 int” 我用过 (int)imgBlob.Lenght 和好的!但是,由于 OracleDbType ,我遇到了错误!如果我使用 .RefCursor,我在 OracleBlob imgBlob = reader.GetOracleBlob(0) 上有错误...关于转换的东西!如果我使用 .Blob,我在 ExecuteReader() 上有错误...关于“一个或多个参数...”的内容 必须使用存储过程吗?我从来没有使用过存储过程的 blob 输出......你的存储过程在做什么?通常,如果您要在数据库中存储一个 blob,则您有一个该 blob 的 ID,并通过该 ID 直接选择/更新/删除。任何过滤/转换都是在数据库之外完成的,您始终可以在数据库中存储 Blob 的其他版本以缓存过滤器/转换,然后您将使用附加列来识别它以上是关于从 Oracle DB 中检索图像的主要内容,如果未能解决你的问题,请参考以下文章