指定转换无效

hhf2006213 2008-05-05 04:40:13
namespace OracleDAL
{
public class OracleProvider:IDAL.IProduce
{

private string connStr = OracleMsgHelper.ConnectionString;

#region IProduce 成员

public void GetContent(out int testValue)
{
testValue=0;
OracleConnection conn = new OracleConnection(connStr);
string sql = "GetTestValue";
OracleCommand comm = new OracleCommand(sql, conn);
OracleParameter pContent =
new OracleParameter("p_testValue", OracleType.Number);
pContent.Direction = ParameterDirection.Output;

comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add(pContent);

try
{
conn.Open();
int count = comm.ExecuteNonQuery();
testValue = (int)pContent.Value;//运行到此处出现指定的转换无效,之前程序都无问题,并且pContent.Value已取到值.另外此程序数据库是oracle,在sql server 2000却不会出现些异常
}
catch
{
}
finally
{
conn.Close();
}
}

#endregion
}
}
...全文
132 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
hhf2006213 2008-05-05
  • 打赏
  • 举报
回复

存储过程如下:
create or replace procedure GetTestValue
(p_testValue out number)
is
v_testValue number(4);
begin
select TestValue into v_testValue from MsgTab
where MsgID=2;
p_testValue:=v_testValue;
end;

最后,逐步调试时,显示取出来的pContent的Value的值为“21”
sun_Ke 2008-05-05
  • 打赏
  • 举报
回复
取出的值是什么?
didamsma 2008-05-05
  • 打赏
  • 举报
回复
具体获取到的是什么值?也许不是数值类型的
要不你在pContent.Value后面加个.toString()看看
wh110 2008-05-05
  • 打赏
  • 举报
回复
SQL里面有时候可以运行的语法在ORACLE里面不见得就是正确的.
wzx516 2008-05-05
  • 打赏
  • 举报
回复
贴出存储过程来看下,可能是里面参数类型的问题。把pContent.Direction = ParameterDirection.Output;改成pContent.Direction=System.Data.ParameterDirection.ReturnValue;看看
hecong875 2008-05-05
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 boblaw 的回复:]
testValue = (int)pContent.Value;
-------------------------------
要防止pContent有可能出現DBNull的情況,除非你確認pContent永遠都會返回一個整數值
[/Quote]
同意5楼说法!
返回的值有可能是NULL 或者不是整数值
hhf2006213 2008-05-05
  • 打赏
  • 举报
回复
用断点,看一下取出来的这个是什么类型
-------------------------------------------------
取出来的是object类型


看看是不是存诸过程中的参数类型定义的有问题
---------------------------------------------------
应该不会有问题,因为程序运行到
int count = comm.ExecuteNonQuery();
都正常,并且pContent.Value也取到了一个正确的object类型的值


testValue = (int)pContent.Value;
-------------------------------
要防止pContent有可能出現DBNull的情況,除非你確認pContent永遠都會返回一個整數值
------------------------------------------------------------------------------
我也做过判断可还是出现上面一样的问题
if (!Convert.IsDBNull(pContent.Value))
{
testValue = (int)pContent.Value;
}
不知道,是不是哪里写错了,请指点,谢谢!!!!还有我想知道为什么在SQL SERVER 里不会出现这种情况????????
boblaw 2008-05-05
  • 打赏
  • 举报
回复
testValue = (int)pContent.Value;
-------------------------------
要防止pContent有可能出現DBNull的情況,除非你確認pContent永遠都會返回一個整數值
kingclever 2008-05-05
  • 打赏
  • 举报
回复
看看是不是存诸过程中的参数类型定义的有问题
didamsma 2008-05-05
  • 打赏
  • 举报
回复
用断点,看一下取出来的这个是什么类型
hhf2006213 2008-05-05
  • 打赏
  • 举报
回复
C# code
//这是什么意思
string sql = "GetTestValue";

------------------------------------------------
调用oracle数据库里的一个存储过程
changjiangzhibin 2008-05-05
  • 打赏
  • 举报
回复

//这是什么意思
string sql = "GetTestValue";
didamsma 2008-05-05
  • 打赏
  • 举报
回复
原因是被转换的对象原本不是Int类型
用(int)转换时必须是与该类型兼容的类型
比如 一个变量 Object A,如果该变量是 int 转换得到,(int)转换时不会出错,
如果是其它类型 ,如 A="123",则转换时将会出错,
而用Convert.ToInt32()则可以将其它类型的变量转换过来,只要是数字
hhf2006213 2008-05-05
  • 打赏
  • 举报
回复
pContent.Value.Tostring()报错吗
-----------------------------------也报错

---------------------------------------------------------
不好意思~~~~~~~~~~~~~~
pContent.Value.ToString()可以,刚才机子重启,服务没开,所以报错
谢谢,各位,有哪位知道,为什么须要这样转换吗?
object类型不是也可以直接强制转化为int类型呀?
blackField 2008-05-05
  • 打赏
  • 举报
回复
用Convert.ToInt32(pContent.Value)试试
hhf2006213 2008-05-05
  • 打赏
  • 举报
回复
pContent.Value.Tostring()报错吗
-----------------------------------也报错
刀枪blue 2008-05-05
  • 打赏
  • 举报
回复
pContent.Value.Tostring()报错吗

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧