Oracle executes parameterized SQL statements and stored procedures

Source: Internet
Author: User
Tags oracleconnection
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. text;
  4. Using system. Data. oracleclient;
  5. Using system. Data;
  6. Namespace oracleopdemo
  7. {
  8. Class Program
  9. {
  10. Private Static string connstr = "Data Source = oratest; user id = m; Password = m123 ;";
  11. Private Static string connstrsql = "Data Source = oratest; user id = CTD; Password = cc ;";
  12. Static void main (string [] ARGs)
  13. {
  14. Executeproc ();
  15. Executeparesql ();
  16. Console. writeline ("execute completed ");
  17. Console. Read ();
  18. }
  19. Private Static void executeparesql ()
  20. {
  21. Using (oracleconnection conn = new oracleconnection (connstrsql ))
  22. {
  23. Try
  24. {
  25. Conn. open ();
  26. String SQL = "insert into ivan_test (ID, name) values (: p_id,: p_name )";
  27. Oraclecommand cmd = new oraclecommand (SQL, Conn );
  28. Oracleparameter para = new oracleparameter ("p_id", oracletype. number );
  29. Para. value = 510;
  30. Cmd. Parameters. Add (para );
  31. Para = new oracleparameter ("p_name", oracletype. varchar );
  32. Para. value = "ivantest ";
  33. Cmd. Parameters. Add (para );
  34. Cmd. executenonquery ();
  35. }
  36. Catch (oracleexception ex)
  37. {
  38. Throw new exception (ex. Message );
  39. }
  40. Finally
  41. {
  42. Conn. Close ();
  43. }
  44. }
  45. }
  46. Private Static void executeproc ()
  47. {
  48. Using (oracleconnection conn = new oracleconnection (connstr ))
  49. {
  50. Try
  51. {
  52. Conn. open ();
  53. Oraclecommand cmd = conn. createcommand ();
  54. Cmd. Connection = conn;
  55. Cmd. commandtype = commandtype. storedprocedure;
  56. Cmd. commandtext = "p_test ";
  57. Oracleparameter para = new oracleparameter ("p_checkid", "09531c6083fc4e579eb376dd9861b299 ");
  58. Cmd. Parameters. Add (para );
  59. Para = new oracleparameter ("p_item", 10 );
  60. Cmd. Parameters. Add (para );
  61. Para = new oracleparameter ("p_value", 11 );
  62. Cmd. Parameters. Add (para );
  63. Para = new oracleparameter ("p_delayday", 1 );
  64. Cmd. Parameters. Add (para );
  65. Para = new oracleparameter ("p_description", "DESC ");
  66. Cmd. Parameters. Add (para );
  67. Cmd. executenonquery ();
  68. }
  69. Catch (oracleexception ex)
  70. {
  71. Throw new exception (ex. Message );
  72. }
  73. Finally
  74. {
  75. Conn. Close ();
  76. }
  77. }
  78. }
  79. }
  80. }

Oracle procedure:

  1. Create or replace procedure Ptest (
  2. P_checkid in varchar2, p_item in number,
  3. P_value in number, p_delayday in number, p_description in varchar2
  4. ) Is
  5. V_state iqc_check.verify_state % type;
  6. Begin
  7. Declare cursor mycursor1 is
  8. Select a. verify_state from check a where a. ID = p_checkid;
  9. Begin
  10. Open mycursor1;
  11. Fetch mycursor1 into v_state;
  12. If v_state = 0 then
  13. Insert into check_pack (checkid, item, value, delayday, description)
  14. Values (p_checkid, p_item, p_value, p_delayday, p_description );
  15. Else
  16. Update check_pack
  17. Set Value = p_value,
  18. Delayday = p_delayday,
  19. Description = p_description
  20. Where checkid = p_checkid and item = p_item;
  21. End if;
  22. Close mycursor1;
  23. End;
  24. -- Commit;
  25. End Ptest;

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.