OleDb ADO.Net ExecuteScalar()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • WeiminZhang

    #1

    OleDb ADO.Net ExecuteScalar()

    When I use the OleDb to connect a Oracle db, and use the
    ExecutScalar() method to get the count of a table, the
    return value can't be cast to a data type, say int, while
    this works fine for a SQL server db using SqlCommand.

    Following is the code example:

    string strCnt = "SELECT count (*) from B_PlanSchemeIte ms";
    string strSource="Prov ider=MSDAORA;Da ta
    Source=avocet1; User ID=dbo; Password=mmsmms ;";
    OleDbConnection conn = new OleDbConnection (strSource);
    conn.Open();

    OleDbCommand cmdCount = new OleDbCommand(st rCnt,conn);
    object objInt;
    objInt = cmdCount.Execut eScalar();
    intNumRecs = (int) (objInt);

    The run time error occurs with message:
    "Specified cast is not valid"

    Following code works fine for a SQL server db:

    string strCnt = "SELECT count (*) from B_PlanSchemeIte ms";
    string
    strSource="serv er=planserv1;ui d=sa;pwd=sysadm in;database=a
    vocet1";
    SqlConnection myConn = new SqlConnection(s trSource);
    myConn.Open();

    SqlCommand cmdCount = new SqlCommand(strC nt, myConn);
    intNumRecs = (int) cmdCount.Execut eScalar();

    Can anyone help me on solving the cast problem in OleDb
    case? Thanks.
  • Ignacio Machin

    #2
    Re: OleDb ADO.Net ExecuteScalar()

    Hi ,

    OleDbCommand.Ex ecuteScalar return an object , not in integer so maybe what
    you are getting is a Decimal and therefore the cast is not valid, this is
    what I would do.
    Assign it to a object variable as you do:
    objInt = cmdCount.Execut eScalar();
    place a breakpoint on this line and see what is the correct type, just to
    know ;)
    I would use Convert.ToInt32 () instead of cast:
    intNumRecs =Convert.ToInt3 2( objInt);

    this should work fine.

    Hope this help,

    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation


    "WeiminZhan g" <wzhang@stssyst ems.com> wrote in message
    news:01ad01c377 c9$7be4f440$a40 1280a@phx.gbl.. .[color=blue]
    > When I use the OleDb to connect a Oracle db, and use the
    > ExecutScalar() method to get the count of a table, the
    > return value can't be cast to a data type, say int, while
    > this works fine for a SQL server db using SqlCommand.
    >
    > Following is the code example:
    >
    > string strCnt = "SELECT count (*) from B_PlanSchemeIte ms";
    > string strSource="Prov ider=MSDAORA;Da ta
    > Source=avocet1; User ID=dbo; Password=mmsmms ;";
    > OleDbConnection conn = new OleDbConnection (strSource);
    > conn.Open();
    >
    > OleDbCommand cmdCount = new OleDbCommand(st rCnt,conn);
    > object objInt;
    > objInt = cmdCount.Execut eScalar();
    > intNumRecs = (int) (objInt);
    >
    > The run time error occurs with message:
    > "Specified cast is not valid"
    >
    > Following code works fine for a SQL server db:
    >
    > string strCnt = "SELECT count (*) from B_PlanSchemeIte ms";
    > string
    > strSource="serv er=planserv1;ui d=sa;pwd=sysadm in;database=a
    > vocet1";
    > SqlConnection myConn = new SqlConnection(s trSource);
    > myConn.Open();
    >
    > SqlCommand cmdCount = new SqlCommand(strC nt, myConn);
    > intNumRecs = (int) cmdCount.Execut eScalar();
    >
    > Can anyone help me on solving the cast problem in OleDb
    > case? Thanks.[/color]


    Comment

    • Weimin Zhang

      #3
      Re: OleDb ADO.Net ExecuteScalar()

      Thanks, Ignacio Machin!

      It works fine now. Looks like the SqlClient object is not
      exactly same as OleDb in the way it returns the object.

      Anyway, thanks again.[color=blue]
      >-----Original Message-----
      >Hi ,
      >
      > OleDbCommand.Ex ecuteScalar return an object , not in[/color]
      integer so maybe what[color=blue]
      >you are getting is a Decimal and therefore the cast is[/color]
      not valid, this is[color=blue]
      >what I would do.
      > Assign it to a object variable as you do:
      > objInt = cmdCount.Execut eScalar();
      >place a breakpoint on this line and see what is the[/color]
      correct type, just to[color=blue]
      >know ;)
      >I would use Convert.ToInt32 () instead of cast:
      > intNumRecs =Convert.ToInt3 2( objInt);
      >
      >this should work fine.
      >
      >Hope this help,
      >
      >--
      >Ignacio Machin,
      >ignacio.mach in AT dot.state.fl.us
      >Florida Department Of Transportation
      >
      >
      >"WeiminZhang " <wzhang@stssyst ems.com> wrote in message
      >news:01ad01c37 7c9$7be4f440$a4 01280a@phx.gbl. ..[color=green]
      >> When I use the OleDb to connect a Oracle db, and use[/color][/color]
      the[color=blue][color=green]
      >> ExecutScalar() method to get the count of a table, the
      >> return value can't be cast to a data type, say int,[/color][/color]
      while[color=blue][color=green]
      >> this works fine for a SQL server db using SqlCommand.
      >>
      >> Following is the code example:
      >>
      >> string strCnt = "SELECT count (*) from[/color][/color]
      B_PlanSchemeIte ms";[color=blue][color=green]
      >> string strSource="Prov ider=MSDAORA;Da ta
      >> Source=avocet1; User ID=dbo; Password=mmsmms ;";
      >> OleDbConnection conn = new OleDbConnection (strSource);
      >> conn.Open();
      >>
      >> OleDbCommand cmdCount = new OleDbCommand(st rCnt,conn);
      >> object objInt;
      >> objInt = cmdCount.Execut eScalar();
      >> intNumRecs = (int) (objInt);
      >>
      >> The run time error occurs with message:
      >> "Specified cast is not valid"
      >>
      >> Following code works fine for a SQL server db:
      >>
      >> string strCnt = "SELECT count (*) from[/color][/color]
      B_PlanSchemeIte ms";[color=blue][color=green]
      >> string
      >>[/color][/color]
      strSource="serv er=planserv1;ui d=sa;pwd=sysadm in;database=a[color=blue][color=green]
      >> vocet1";
      >> SqlConnection myConn = new SqlConnection(s trSource);
      >> myConn.Open();
      >>
      >> SqlCommand cmdCount = new SqlCommand(strC nt, myConn);
      >> intNumRecs = (int) cmdCount.Execut eScalar();
      >>
      >> Can anyone help me on solving the cast problem in OleDb
      >> case? Thanks.[/color]
      >
      >
      >.
      >[/color]

      Comment

      Working...