Solved

Alter Oracle Sequence in C#

Posted on 2013-05-22
2
1,003 Views
Last Modified: 2013-05-22
I need to alter an Oracle Sequence in my code (C#) and I keep receiving an Invalid Cast error.  I've tried casting to INT64, INT32 and just INT and still get the same error.  Here is my code:

        public Boolean resetSEQ(OracleConnection connectionObj, String owner, String seqname)
        {
            Int64 currVal = 0;
            Boolean success = true;
            OracleCommand command = connectionObj.CreateCommand();
            try
            {
                // Reset Sequence
                ErrMsg = "";

                // Check that the database has been opened
                if (connectionObj == null || connectionObj.State != ConnectionState.Open)
                {
                    ErrMsg = "<br />Database is not opened.";
                    return false;
                }

                // Create Command object
                command.Connection = connectionObj;

                try
                {
                    // Get Current Sequence Value
                    command.CommandText =
                        "SELECT " + owner + "." + seqname + ".nextval FROM dual";

                    currVal = (int)command.ExecuteScalar();
                    ErrMsg = ErrMsg + currVal.ToString();
                }
                catch (Exception ex)
                {
                    ErrMsg = ErrMsg + "<br />Error Retrieving Next Sequence for " + seqname + ": " + ex.Message;
                    success = false;
                }
                //try
                //{
                //    // Get Current Sequence Value
                //    command.CommandText =
                //        "SELECT " + owner + "." + seqname + ".currval FROM dual";

                //    currVal = (Int64)command.ExecuteScalar();
                //    ErrMsg = ErrMsg + currVal.ToString();
                //}
                //catch (Exception ex)
                //{
                //    ErrMsg = ErrMsg + "<br />Error Retrieving Current Sequence for " + seqname + ": " + ex.Message;
                //    success = false;
                //}
            }
            catch (Exception ex)
            {
                ErrMsg = ErrMsg + "<br />ERROR resetSEQ: " + ex.Message;
                success = false;
            }
            finally
            {
                command.Dispose();
            }
            return success;
        }

Open in new window


The code only shows trying to get the current value of the sequence.  If I can get this working I can get the rest working.

Any help or suggestions would be greatly appreciated!
0
Comment
Question by:dyarosh
2 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Have you tried double or decimal? The numeric types coming back from Oracle, IMO, are not very intuitive. You might also try putting a breakpoint on the ExecuteScalar line and executing the following in the Immediate Window to check the type:

?command.ExecuteScalar().GetType()

Open in new window

0
 

Author Closing Comment

by:dyarosh
Comment Utility
Casting as type Decimal worked.  Thanks.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now