ExecuteOracleScalar error??

  • Thread starter Thread starter daiyuan
  • Start date Start date
D

daiyuan

Hi:

When I run this project,I miss a error:

it's say: convert is falied!

I don't konw why.

Please advice.

Thanks!!!

This is my codes:


////////////////////////////////////////////////////////////////////////////
///

using System.Data.OracleClient;

....

private int SetVirtualItemCount()
{
int result = 0;

// database connection
String strConn = "Password=;User ID=sa;Data Source=HBMIS";
OracleConnection conn = new OracleConnection(strConn);

// ¼ÆËã¼Í¼×ÜÊý
String strCmd = "SELECT COUNT(*) FROM Employees";
OracleCommand cmd = new OracleCommand(strCmd,conn);

// Ö´ÐÐ
conn.Open();
int count = (int)cmd.ExecuteOracleScalar(); //error
line
conn.Close();

//result = (int)count;

return result;
}
////////////////////////////////////////////////////////////////////////
 
DaiYuan,
ExecuteOracleScalar() returns Oracle specific DataType.Since you are
expecting an integer, the equivalent Oracle type is OracleNumber. Hence your
code should look like this:
Code Snippet:
oraclecommand1.CommandText = "select Count(*) from Employees";
OracleNumber oraclenumber1 =
(OracleNumber)oraclecommand1.ExecuteOracleScalar();

If all you want is to get an integer value, use ExecuteScalar method instead
Code Snippet:
oraclecommand1.CommandText = "select Count(*) from Employees";
int count = (int)oraclecommand1.ExecuteScalar();

Hope That Helps,
Sushil.
 
Hi:

When I run this project,I miss a error:

it's say: convert is falied!

I don't konw why.

Please advice.

Thanks!!!

This is my codes:


////////////////////////////////////////////////////////////////////////////
///

using System.Data.OracleClient;

....

private int SetVirtualItemCount()
{
int result = 0;

// database connection
String strConn = "Password=;User ID=sa;Data Source=HBMIS";
OracleConnection conn = new OracleConnection(strConn);

// ¼ÆËã¼Í¼×ÜÊý
String strCmd = "SELECT COUNT(*) FROM Employees";
OracleCommand cmd = new OracleCommand(strCmd,conn);

// Ö´ÐÐ
conn.Open();
int count = (int)cmd.ExecuteOracleScalar(); //error
line
conn.Close();

//result = (int)count;

return result;
}
////////////////////////////////////////////////////////////////////////

zam
*********************************
int count = (int)cmd.ExecuteOracleScalar(); //error

answer

Int32 count = Convert.ToInt32(cmd.ExecuteOracleScalar());
 
Back
Top