本文介绍了如何解决错误“无法将类型为'system.int16'的对象转换为'system.byte []'。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图从oracle数据库中检索图像。 i有一个网页表单,我希望在点击提交按钮后显示图像。 我遇到的问题是它抛出错误说Im trying to retrieve an image from an oracle DB.i have a web form where i want the image to display once i click on the submit button.the problem i'm having is it throws an error saying "Unable to cast object of type 'System.Int16' to type 'System.Byte[]'. 一旦到达" once it gets to context.Response.BinaryWrite((byte[])dr); part and如果我发表评论完成图片没有出现在页面上。 谢谢 我尝试过: public void ProcessRequest(HttpContext context) { 尝试 { string cusNum = context.Request.QueryStrin g [cus_Num]。ToString(); string braCode = context.Request.QueryString [bra_Code]。ToString(); OracleConnection OracleConn =新的OracleConnection(oracle); OracleConn.Open(); string query =select * from Acct_sig其中CUS_NUM =:CUS_NUM和BRA_CODE = :BRA_CODE和SIG_SEQ =:SIG_SEQ; OracleCommand OraC =新的OracleCommand(查询,OracleConn); OraC.CommandType = CommandType.Text; OraC.Parameters.Add(new OracleParameter(CUS_NUM,OracleDbType.Varchar2,ParameterDirection.Input))。Value = cusNum; OraC.Parameters.Add(new OracleParameter(BRA_CODE, OracleDbType.Varchar2,ParameterDirection.Input))。Value = braCode; OraC.Parameters.Add(new OracleParameter(SIG_SEQ,OracleDbType.Decimal,ParameterDirection.Input))。Value = 1; b $ b 对象dr = OraC.ExecuteScalar(); OracleConn.Close(); OracleConn.Dispose(); context.Response.BinaryWrite((byte [])dr); } catch(Exception ex) { ex.ToString(); } } part and when if i comment it out complete the image does not appear on the page.thank youWhat I have tried:public void ProcessRequest (HttpContext context) { try { string cusNum = context.Request.QueryString["cus_Num"].ToString(); string braCode = context.Request.QueryString["bra_Code"].ToString(); OracleConnection OracleConn = new OracleConnection(oracle); OracleConn.Open(); string query = "select * from Acct_sig where CUS_NUM= :CUS_NUM and BRA_CODE= :BRA_CODE and SIG_SEQ= :SIG_SEQ"; OracleCommand OraC = new OracleCommand(query, OracleConn); OraC.CommandType = CommandType.Text; OraC.Parameters.Add(new OracleParameter("CUS_NUM", OracleDbType.Varchar2, ParameterDirection.Input)).Value = cusNum; OraC.Parameters.Add(new OracleParameter("BRA_CODE", OracleDbType.Varchar2, ParameterDirection.Input)).Value = braCode; OraC.Parameters.Add(new OracleParameter("SIG_SEQ", OracleDbType.Decimal, ParameterDirection.Input)).Value = 1; object dr = OraC.ExecuteScalar(); OracleConn.Close(); OracleConn.Dispose(); context.Response.BinaryWrite((byte[])dr); } catch (Exception ex) { ex.ToString(); } }推荐答案 ExecuteScalar返回单个值:在这种情况下,它返回第一行的第一个值。这取决于数据库设计,如果更改了数据库表,则可能会发生变化。 如果需要单个特定值,请更改查询以返回单个特定值: ExecuteScalar returns a single value: in this case it returns the first value of the first row. What that is will depend on the DB design, and is subject to change if the DB table is changed.If you want a single, specific value, then change your query to return a single specific value:SELECT UserID FROM Mytable WHERE UserName='Emmablakes'否则,使用DataReader或DataAdapter返回多个值或多行。Otherwise, use a DataReader or a DataAdapter to return multiple values or multiple rows. 这篇关于如何解决错误“无法将类型为'system.int16'的对象转换为'system.byte []'。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-22 11:21