Solved

C#, Oracle .NET Update Datefield

Posted on 2009-05-04
2
861 Views
Last Modified: 2012-05-06
Hello experts,

I am trying update one of my oracle table. one field is a datatime. How do I do that?

PPeriodStart - it is datetime
PPeriodEnd - it is datetime


                    OracleTransaction myOracleTransaction = Program.g_DBO.cnDB.BeginTransaction(); ;
                    OracleCommand oraCmd = new OracleCommand();
                    try
                    {
                        sqlText = "UPDATE pc.pcy set " +
                                " PAC = " + PcyAss + ", " +
                                " PPeriodStart = '" + To_date(PPeriodStart, "yyyymmdd") + "', " +
                                " PPeriodEnd = '" + To_date(PPeriodEnd, "yyyymmdd") + "'
                                " WHERE CoverageID = " + CoverageID + "";
                        oraCmd.Connection = Program.g_DBO.cnDB;
                        oraCmd.CommandType = CommandType.Text;
                        oraCmd.CommandText = sqlText;
                        oraCmd.ExecuteNonQuery();
                        myOracleTransaction.Commit();
                        return true;
0
Comment
Question by:listings_
2 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24295629
You can use bind variables, and add parameters via OracleCommand.Parameters.Add

Somethign like this (untested), I don't have my VS up with a project to test.

Also read:


http://msdn.microsoft.com/en-us/library/system.data.oracleclient.oracleparameter.aspx

http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28844/building_odp.htm#CEGCGDAB

http://download.oracle.com/docs/html/B28089_01/OracleParameterClass.htm#i1010881

                    OracleTransaction myOracleTransaction = Program.g_DBO.cnDB.BeginTransaction(); ;
                    OracleCommand oraCmd = new OracleCommand();
                    try
                    {
                        sqlText = "UPDATE pc.pcy set PAC = " + PcyAss + ", " +  // I would also convert PcyAss to a bind parameter
                                " PPeriodStart = :PPeriodStart, " +
                                " PPeriodEnd = :PPeriodEnd " +
                                " WHERE CoverageID = :CoverageID ";
                        oraCmd.Connection = Program.g_DBO.cnDB;
                        oraCmd.CommandType = CommandType.Text;
                        oraCmd.CommandText = sqlText;
                        oraCmd.Parameters.Add("PPeriodStart", OracleType.Date, PPeriodStart, ParameterDirection.Input);
                        oraCmd.Parameters.Add("PPeriodEnd", OracleType.Date, PPeriodEnd, ParameterDirection.Input);
                        oraCmd.Parameters.Add("CoverageID", OracleType.Int, CoverageID, ParameterDirection.Input);
                        oraCmd.ExecuteNonQuery();
                        myOracleTransaction.Commit();
                        return true;

Open in new window

0
 

Author Comment

by:listings_
ID: 24297615
Thanks mrjoltcola.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

820 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