CINXE.COM
Getting Started with ODP.Net Core
<!DOCTYPE html> <html lang="en"> <head> <meta content="text/html; charset=utf-8" http-equiv="content-type"> <!-- Part number: F10045-01 --> <!-- Published date: September 2018 --> <!-- See this link for directions on getting a part number: https://confluence.oraclecorp.com/confluence/display/DLPP/Archive+an+OBE --> <!-- Template date: 10/18/17 --> <title>Getting Started with ODP.Net Core</title> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="robots" content="INDEX, FOLLOW"> <meta name="description" content="Put the description of the tutorial here."> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="css/normalize.min.css"> <link rel="stylesheet" href="css/font-awesome.min.css"> <link rel="stylesheet" href="css/obe-lite.css"> <script src="js/jquery-1.11.0.min.js"></script> <script src="js/jquery-ui-1.10.4.custom.js"></script> <script src="js/jquery.tocify.jd.js"></script> <script src="js/leftnav.js"></script> <script>(window.BOOMR_mq=window.BOOMR_mq||[]).push(["addVar",{"rua.upush":"false","rua.cpush":"false","rua.upre":"false","rua.cpre":"false","rua.uprl":"false","rua.cprl":"false","rua.cprf":"false","rua.trans":"","rua.cook":"false","rua.ims":"false","rua.ufprl":"false","rua.cfprl":"false","rua.isuxp":"false","rua.texp":"norulematch","rua.ceh":"false","rua.ueh":"false","rua.ieh.st":"0"}]);</script> <script>!function(e){var n="https://s.go-mpulse.net/boomerang/";if("False"=="True")e.BOOMR_config=e.BOOMR_config||{},e.BOOMR_config.PageParams=e.BOOMR_config.PageParams||{},e.BOOMR_config.PageParams.pci=!0,n="https://s2.go-mpulse.net/boomerang/";if(window.BOOMR_API_key="DXNLE-YBWWY-AR74T-WMD99-77VRA",function(){function e(){if(!o){var e=document.createElement("script");e.id="boomr-scr-as",e.src=window.BOOMR.url,e.async=!0,i.parentNode.appendChild(e),o=!0}}function t(e){o=!0;var n,t,a,r,d=document,O=window;if(window.BOOMR.snippetMethod=e?"if":"i",t=function(e,n){var t=d.createElement("script");t.id=n||"boomr-if-as",t.src=window.BOOMR.url,BOOMR_lstart=(new Date).getTime(),e=e||d.body,e.appendChild(t)},!window.addEventListener&&window.attachEvent&&navigator.userAgent.match(/MSIE [67]\./))return window.BOOMR.snippetMethod="s",void t(i.parentNode,"boomr-async");a=document.createElement("IFRAME"),a.src="about:blank",a.title="",a.role="presentation",a.loading="eager",r=(a.frameElement||a).style,r.width=0,r.height=0,r.border=0,r.display="none",i.parentNode.appendChild(a);try{O=a.contentWindow,d=O.document.open()}catch(_){n=document.domain,a.src="javascript:var d=document.open();d.domain='"+n+"';void(0);",O=a.contentWindow,d=O.document.open()}if(n)d._boomrl=function(){this.domain=n,t()},d.write("<bo"+"dy onload='document._boomrl();'>");else if(O._boomrl=function(){t()},O.addEventListener)O.addEventListener("load",O._boomrl,!1);else if(O.attachEvent)O.attachEvent("onload",O._boomrl);d.close()}function a(e){window.BOOMR_onload=e&&e.timeStamp||(new Date).getTime()}if(!window.BOOMR||!window.BOOMR.version&&!window.BOOMR.snippetExecuted){window.BOOMR=window.BOOMR||{},window.BOOMR.snippetStart=(new Date).getTime(),window.BOOMR.snippetExecuted=!0,window.BOOMR.snippetVersion=12,window.BOOMR.url=n+"DXNLE-YBWWY-AR74T-WMD99-77VRA";var i=document.currentScript||document.getElementsByTagName("script")[0],o=!1,r=document.createElement("link");if(r.relList&&"function"==typeof r.relList.supports&&r.relList.supports("preload")&&"as"in r)window.BOOMR.snippetMethod="p",r.href=window.BOOMR.url,r.rel="preload",r.as="script",r.addEventListener("load",e),r.addEventListener("error",function(){t(!0)}),setTimeout(function(){if(!o)t(!0)},3e3),BOOMR_lstart=(new Date).getTime(),i.parentNode.appendChild(r);else t(!1);if(window.addEventListener)window.addEventListener("load",a,!1);else if(window.attachEvent)window.attachEvent("onload",a)}}(),"".length>0)if(e&&"performance"in e&&e.performance&&"function"==typeof e.performance.setResourceTimingBufferSize)e.performance.setResourceTimingBufferSize();!function(){if(BOOMR=e.BOOMR||{},BOOMR.plugins=BOOMR.plugins||{},!BOOMR.plugins.AK){var n=""=="true"?1:0,t="",a="bdpnbeqxjpkyoz2cxaea-f-beefa9ac5-clientnsv4-s.akamaihd.net",i="false"=="true"?2:1,o={"ak.v":"39","ak.cp":"82485","ak.ai":parseInt("604074",10),"ak.ol":"0","ak.cr":3,"ak.ipv":4,"ak.proto":"http/1.1","ak.rid":"704f8ea8","ak.r":42017,"ak.a2":n,"ak.m":"dscx","ak.n":"essl","ak.bpcip":"8.222.208.0","ak.cport":58174,"ak.gh":"184.28.235.13","ak.quicv":"","ak.tlsv":"tls1.2","ak.0rtt":"","ak.0rtt.ed":"","ak.csrc":"-","ak.acc":"reno","ak.t":"1732425736","ak.ak":"hOBiQwZUYzCg5VSAfCLimQ==OORHLNp9HIf70HrYTobJXNmkoV/3F6DDpEAFWnClxtmU/fspOjx1hFUjKxV5cZ5E7jlWmlBIjzhVvNmH4wk2RmNbJX8dy7DtsEMYblFjFYGLayJJ3ZnlUq2nCtJiJUpct6/kCGR0O3NNu45mQMRq3YQL5dObBDJmfCmKsffnijT8odKesLkDq5KtjOKrbZqp4wDNgm16h+UgBrXvABwHMRbkOcbz7MxxcXON8YUL1jlPPNcK1UKv05RqabMcCUtZhzN/QpY0K0BjhKDjBu2863oqGBTp78BNxxxnATvWWU2M5o+zx9J9yIzO9EOksm0t+5f980+HbfsWMfFFb20wKHFS1HCztu0+c90lspOntJLBw9rDm2x9CMzScqK9MLs0ddCPpYSAXPtrKGH/qNuihTSkzVyJv7Cx8hSbfXMm8MY=","ak.pv":"492","ak.dpoabenc":"","ak.tf":i};if(""!==t)o["ak.ruds"]=t;var r={i:!1,av:function(n){var t="http.initiator";if(n&&(!n[t]||"spa_hard"===n[t]))o["ak.feo"]=void 0!==e.aFeoApplied?1:0,BOOMR.addVar(o)},rv:function(){var e=["ak.bpcip","ak.cport","ak.cr","ak.csrc","ak.gh","ak.ipv","ak.m","ak.n","ak.ol","ak.proto","ak.quicv","ak.tlsv","ak.0rtt","ak.0rtt.ed","ak.r","ak.acc","ak.t","ak.tf"];BOOMR.removeVar(e)}};BOOMR.plugins.AK={akVars:o,akDNSPreFetchDomain:a,init:function(){if(!r.i){var e=BOOMR.subscribe;e("before_beacon",r.av,null,null),e("onbeacon",r.rv,null,null),r.i=!0}return this},is_complete:function(){return!0}}}}()}(window);</script></head> <body> <header> <div class="w1024" style="min-height:55px; display:block;"> <a href="https://docs.oracle.com" class="oracle-logo"> <img src="./img/oracle_doc_logo.png" alt="Oracle Documentation" style="max-width: none;" height="22" width="236"></a> </div> </header> <!--end header--> <div id="content"> <h1><img src="./img/obe_tag.png" alt="Oracle by Example branding" align="middle">Getting Started with ODP.NET Core</h1> <div class="w1024" style="clear:both; position:relative; margin-top:40px;"> <div class="navbackwide border-right"><!-- --></div> <div id="shownav" title="Show Navigation" tabindex="0"><span class="fa fa-list"></span></div> <div id="sidebar"><!-- --> <div class="left-nav-tut"><!-- --> <div id="hidenavw" title="Hide Navigation" tabindex="0"><span class="fa fa-close"></span></div> <div id="navbar" class="left-nav-w"><!-- --> <div id="toc" class="tocify"><!-- --></div> </div> </div> </div> <div id="bookContainer"> <article> <section> <h2> <img src="./img/32_begin.png" alt="section 0" class="num_circ" height="32" width="32">Before You Begin </h2> <p> This 15-minute tutorial shows you how to create a basic .NET Core web application that retrieves data from the Oracle database with Oracle Data Provider for .NET (ODP.NET) Core. </p> <h3>Background</h3> <p> ODP.NET Core is an ADO.NET driver that provides fast data access from Microsoft .NET Core clients to Oracle databases. It runs on both Windows and Linux. ODP.NET consists of a single 100% managed code dynamic-link library, Oracle.ManagedDataAccess.dll, available via NuGet deployment. </p> <p> In this tutorial, you will learn how to create an ASP.NET Core web application, download ODP.NET Core from nuget.org, configure ODP.NET using the OracleConfiguration class, and connect to the Oracle database. </p> <h3>What Do You Need?</h3> <p> Before starting this tutorial, you should:</p> <ul> <li>Install Microsoft Visual Studio 2017 or later.</li> <li>Install Oracle Database 12c or later.</li> <li>Download the <a href="./files/source_files.zip">source_files.zip</a> file.</li> </ul> </section> <!-- ========================================================================================================================= --> <section> <hr> <h2 id="section_1" role="button" style="text-align: left;"><img src="./img/32_1.png" alt="section 1" class="num_circ" height="32" width="32">Create ASP.NET Core Web Project in Visual Studio</h2> <ol> <li>Open Visual Studio. Click '<strong>File</strong>'. Select <strong>New>Project</strong>.<br> <figure><img src="./img/img1_1.jpg" alt="Creating a new project in Visual Studio" class="ScreenShot"> <figcaption><a href="./files/img1_1.txt">Description of the illustration [filename]</a></figcaption> </figure> </li> <li>Select <span style="font-weight: bold;">Visual C#>.NET Core> ASP.NET Core Web Application</span>. Rename the Web Application as <span style="font-weight: bold;">First_Web_Application</span> and click <span style="font-weight: bold;">OK</span>. <br> <figure><img src="./img/img1_2.jpg" alt="Selecting .NET Core and ASP.NET Core Web Application in the wizard " class="ScreenShot"> <figcaption><a href="./files/img1_2.txt">Description of the illustration [filename]</a></figcaption> </figure> </li> <li>Select <span style="font-weight: bold;">Empty</span> Project template and click <span style="font-weight: bold;">OK</span>. <figure><img src="./img/img1_3.jpg" alt="Select Empty in the wizard" class="ScreenShot"> <figcaption><a href="./files/img1_3.txt">Description of the illustration [filename]</a></figcaption> </figure> </li> <li><span style="font-weight: bold;">First_Web_Application</span> is created, you can explore the structure of the project in the Solution Explorer. <figure><img src="./img/img1_4.jpg" alt="You see the GUI with the program.cs file open and the Solution Explorer" class="ScreenShot"> <figcaption><a href="./files/img1_4.txt">Description of the illustration [filename]</a></figcaption> </figure> </li> </ol> </section> <hr> <section> <h2 id="section_2" role="button" style="text-align: left;"><img src="./img/32_2.png" alt="section 2" class="num_circ" height="32" width="32">Download ODP.NET Core from Nuget.org</h2> <p> In this section, you will download the ODP.NET Core NuGet package into the application.</p> <ol> <li>Right Click on <span style="font-weight: bold;">Dependencies</span> in the <span style="font-weight: bold;">Solution Explorer</span> and Select <span style="font-weight: bold;">Manage NuGet packages</span>. <figure><img src="./img/img2_1.jpg" alt="Solution Explorer with the right click menu. Select Manage Nuget Packages in the menu" class="ScreenShot"> <figcaption><a href="./files/img2_1.txt">Description of the illustration [filename]</a></figcaption> </figure> </li> <li>Select <span style="font-weight: bold;">Browse</span> and search for <span style="font-weight: bold;">Oracle.ManagedDataAccess.Core</span>. <figure><img src="./img/img2_2.jpg" alt="Search for Oracle.ManagedDataAccess.Core in the NuGet Package manager" class="ScreenShot"> <figcaption><a href="./files/img2_2.txt">Description of the illustration [filename]</a></figcaption> </figure> </li> <li>Select <strong>Oracle.ManagedDataAccess.Core</strong> and Click <strong>Install</strong>. <figure><img src="./img/img2_3.jpg" alt="Install the Oracle.ManagedDataAccess.Core pacakge into the Web application" class="ScreenShot"> <figcaption><a href="./files/img2_3.txt">Description of the illustration [filename]</a></figcaption> </figure> </li> <li>Click <span style="font-weight: bold;">OK</span> to start the installation. <figure><img src="./img/img2_4.jpg" alt="Click OK in the preview changes wizard" class="ScreenShot"> <figcaption><a href="./files/img2_4.txt">Description of the illustration [filename]</a></figcaption> </figure> </li> <li>Accept the license. <figure><img src="./img/img2_5.jpg" alt="Click OK for the license acceptance" class="ScreenShot"> <figcaption><a href="./files/img2_5.txt">Description of the illustration [filename]</a></figcaption> </figure> </li> <li>You can see the installed package in the Solution Explorer. <figure><img src="./img/img2_6.jpg" alt="You will find the Oracle.ManagedDataAccess.Core in the Solution Explorer" class="ScreenShot"> <figcaption><a href="./files/img2_6.txt">Description of the illustration [filename]</a></figcaption> </figure> </li> </ol> </section> <hr> <section> <h2 id="section_3" role="button" style="text-align: left;"> <img src="./img/32_3.png" alt="section 3" class="num_circ" height="32" width="32">Add ODP.NET Core Namespace and Code</h2> <p> In this section, we will configure the ODP.NET Core namespace and set up the data access code. </p> <ol> <li> Open the Startup_cs.txt file in <a href="./files/source_files.zip">source_files.zip</a>. You will replace the<span style="font-weight: bold;"> app.Run </span> function definition in the application's startup.cs with this code. To copy, highlight all the text in the file and type <b>Ctrl-C</b>. </li> <li>Open <span style="font-weight: bold;">startup.cs</span> in your Visual Studio ASP.NET Core project. Add a reference to <span style="font-weight: bold;">Oracle.ManagedDataAccess.Client</span> namespace near the top of the file. <figure> <img src="./img/img3_2.jpg" alt="Add Oracle.ManagedDataAccess.Client into the libraries used in the application" class="ScreenShot"> <figcaption> <a href="./files/img3_2.txt"> Description of the illustration [filename] </a> </figcaption> </figure> </li> <li>Highlight the <b>App.Run</b> code segment in the Configure method in web application's <span style="font-weight: bold;">startup.cs</span> file. <figure> <img src="./img/img3_3.jpg" alt="app.Run code in programstartup. cs file" class="ScreenShot"> <figcaption> <a href="./files/img3_3.txt"> Description of the illustration [filename] </a> </figcaption> </figure> </li> <li>Replace the <span style="font-weight: bold;">app.Run</span> code segment with the copied code by highlighting the current <span style="font-weight: bold;">app.Run</span> section, then typing <b>Ctrl-V</b> to paste. This code retrieves all the first names of employees in department 50 and outputs them to the web page. <figure> <img src="./img/img3_4.jpg" alt="Part of the modified code in the app.Run function" class="ScreenShot"> <figcaption> <a href="./files/img3_4.txt"> Description of the illustration [filename] </a> </figcaption> </figure> <figure> <img src="./img/img3_5.jpg" alt="Part of the app.Run function" class="ScreenShot"> <figcaption> <a href="./files/img3_5.txt"> Description of the illustration [filename] </a> </figcaption> </figure> </li> </ol> </section> <hr> <section> <h2 id="section_4" role="button" style="text-align: left;"><img src="./img/32_4.png" alt="section 4" class="num_circ" height="32" width="32">Use Easy Connect to Setup Database Connection</h2> <p> Easy Connect provides a simple way to connect to an Oracle database without any configuration. </p> <ol> <li>Update the <b>User Id</b> and <b>Password</b> with the database user credentials. Populate the <b>Data Source</b> attribute using the Easy Connect format that corresponds to your database: <b><hostname or IP address>:<listener port>/<database service name></b>. For example, <br> <span style="font-weight: bold;">Hostname</span> = localhost <br> <span style="font-weight: bold;">Port</span> = 1521 <br> <span style="font-weight: bold;">Service name</span> = orcl <figure> <img src="./img/img4_1.jpg" alt="Credentials required to access the data source added to the code" class="ScreenShot"> <figcaption> <a href="./files/img4_1.txt"> Description of the illustration [filename] </a> </figcaption> </figure> </li> </ol> </section> <hr> <section> <h2 id="section_5" role="button" style="text-align: left;"><img src="./img/32_5.png" alt="section 5" class="num_circ" height="32" width="32">Run the Web Application</h2> <ol> <li>You have completed all the steps needed for a basic ODP.NET web application. Run the web application by clicking on the green triangle on the menu bar. <figure><img src="./img/img5_1.jpg" alt="Execute the application by clicking on Run button" class="ScreenShot"> <figcaption><a href="./files/img5_1.txt">Description of the illustration [filename]</a></figcaption> </figure> </li> <li>The output is all the first names of employees in department 50. <figure><img src="./img/img5_2.jpg" alt="Query result displayed on the web page" class="ScreenShot"> <figcaption><a href="./files/img5_2.txt">Description of the illustration [filename]</a></figcaption> </figure> </li> </ol> </section> <hr> <section> <h2 id="section_6" role="button" style="text-align: left;"> <img src="./img/32_6.png" alt="section 6" class="num_circ" height="32" width="32">Add Configuration API Settings </h2> <p> In this section, you will learn about how to use ODP.NET Configuration API to configure ODP.NET programmatically. </p> <ol> <li> Open the config-api.txt file in <a href="./files/source_files.zip">source_files.zip</a>. You will insert this code that programmatically configures ODP.NET Core. To copy, highlight all the text in the file and type <b>Ctrl-C</b>. <figure> <img src="./img/img6_2.jpg" alt="The code to be added to the application to use Configuration API" class="ScreenShot"> <figcaption> <a href="./files/img6_2.txt"> Description of the illustration [filename] </a> </figcaption> </figure> </li> <li>Paste (<b>Ctrl-V</b>) the code at the beginning of the try block in <span style="font-weight: bold;">startup.cs</span> file. These Configuration API settings must be set before the ODP.NET connection is opened. Review the ODP.NET Configuration API settings. You see how different ODP.NET settings are modified, including adding net service names and connect descriptors. <figure> <img src="./img/img6_3.jpg" alt="Location in the code where we add Configuration API specific code" class="ScreenShot"> <figcaption> <a href="./files/img6_3.txt"> Description of the illustration [filename] </a> </figcaption> </figure> </li> </ol> </section> <hr> <section> <h2 id="section_7" role="button" style="text-align: left;"> <img src="./img/32_7.png" alt="section 7" class="num_circ" height="32" width="32">Use Configuration API Connection Settings </h2> <p> In this section, we will modify the connection information to switch from using Easy Connect to a net service name defined in the Configuration API. </p> <ol> <li>Modify the connection string <b>Data Source</b> by replacing the Easy Connect information with <b>orclpdb</b>. <br> </li> <li>The first line of Configuration API code added configures the <b>orclpdb</b> net service name. Modify the connect descriptor information on that line by modifying the <b>HOST</b>, <b>PORT</b>, and <b>SERVICE_NAME</b> entries that correspond to your database. <figure> <img src="./img/img7.jpg" alt="Modified data source in the code" class="ScreenShot"> <figcaption> <a href="./files/img7.txt"> Description of the illustration [filename] </a> </figcaption> </figure> </li> </ol> </section> <hr> <section> <h2 id="section_8" role="button" style="text-align: left;"><img src="./img/32_8.png" alt="section 8" class="num_circ" height="32" width="32">Run the Web Application</h2> <p> You have completed all the steps to configure the database connection using Configuration API. </p> <ol> <li>Execute the web application. <figure><img src="./img/img8_1.jpg" alt="Run the web application" class="ScreenShot"> <figcaption><a href="./files/img8_1.txt">Description of the illustration [filename]</a></figcaption> </figure> </li> <li>You can see the database output on the web page. <figure><img src="./img/img8_2.jpg" alt="Output displayed on the webpage" class="ScreenShot"> <figcaption><a href="./files/img8_2.txt">Description of the illustration [filename]</a></figcaption> </figure> </li> </ol> </section> <hr> <section> <!-- Include the More Information section only in a single or the final lab or tutorial in a series --> <h2 id="more_info" role="button" style="text-align: left;"> <img src="./img/32_more.png" alt="more information" class="num_circ" height="32" width="32">Want to Learn More? </h2> <ul> <li><a href="https://www.oracle.com/database/technologies/appdev/dotnet.html" target="_blank">Oracle .NET Developer Center</a></li> <li><a href="https://apexapps.oracle.com/pls/apex/f?p=44785:24:0::NO:24:P24_CONTENT_ID:13260" target="_blank">Oracle .NET OBE Tutorials</a></li> <li><a href="https://www.youtube.com/user/OracleDOTNETTeam" target="_blank">Oracle .NET video tutorials</a></li> <li><a href="http://www.oracle.com/technetwork/topics/dotnet/tech-info/dotnetcloud-3112651.html" target="_blank">Oracle .NET Cloud Tutorials</a></li> </ul> </section> <hr> </article> </div> <br class="clearfloat"> </div> </div> <!--close main--> <!--end content--> <div class="footer-container "> <div style="max-width: 994px; padding:10px 0 0 17px;"> <footer class="footer-list"> <ul> <li> <a href="https://www.oracle.com/corporate/index.html" target="_blank">About Oracle</a> </li> <li> <a href="https://www.oracle.com/us/corporate/contact/index.html" target="_blank">Contact Us</a> </li> <li> <a href="https://www.oracle.com/us/legal/index.html" target="_blank">Legal Notices</a> </li> <li> <a href="https://www.oracle.com/us/legal/terms/index.html" target="_blank">Terms of Use</a> </li> <li> <a href="https://www.oracle.com/us/legal/privacy/index.html" target="_blank">Your Privacy Rights</a> </li> <li><a href="https://www.oracle.com/pls/topic/lookup?ctx=cpyr&id=en">Copyright 漏 2018, Oracle and/or its affiliates. All rights reserved.</a></li> </ul> </footer> </div> <script type="text/javascript" src="https://www.oracleimg.com/us/assets/metrics/ora_docs.js"></script> </div> </body> </html>