Solved

C#, Oracle .NET Update Datefield

Posted on 2009-05-04
2
856 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

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…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to recover a database from a user managed backup

932 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

11 Experts available now in Live!

Get 1:1 Help Now