.NET
  Home arrow .NET arrow Page 3 - Using Microsoft .NET and C# with Oracle 9i
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Using Microsoft .NET and C# with Oracle 9i
By: Michael Swannson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 41
    2004-10-20

    Table of Contents:
  • Using Microsoft .NET and C# with Oracle 9i
  • Connecting to a Database
  • Creating a Command
  • Creating a DataSet from and Oracle Database

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using Microsoft .NET and C# with Oracle 9i - Creating a Command


    (Page 3 of 4 )


     
    In this section, we look at creating the main workhorse class and its attributes and capabilities. The OracleCommand class has two main functions. First, you can give it a simple SQL query string and execute that command on the database. Secondly, you can use the OracleCommand object to execute a stored procedure on the database. 

    The query string is the simplest use of this command and we will look at it first. 

    First, create the command by using the factory method CreateCommand() method on the OracleConnection object.
     
     OracleCommand cmd = conn.CreateCommand();

    This creates a command object attached to the conn connection.  Alternatively, the command object can be created using its own constructor and then set the Connection field later:
     
     OracleCommand cmd = new OracleCommand();
     cmd.Connection = conn;

    After creating the command, we must set the CommandText attribute to give the object the actual string containing the SQL query it will run on the database. The code below also sets the CommandType attribute, which selects whether CommandText is a text command or the name of a stored procedure (this defaults to Text):

     String query = “select * from users”;
     cmd.CommandText = query;
     cmd.CommandType = CommandType.Text;

    The last step to complete is to run the command on the database and catch the results in an OracleDataReader object.
     
     OracleDataReader reader = cmd.ExecuteReader();

    This executes the command on the database and puts the results in reader. There is an important distinction between queries that read data and those that only update data. When your command reads data, you use the ExecuteReader() method to get this data back. However, when all your command does is insert or update data with nothing meaningful to return, you would use the ExecuteNonQuery() method which we will cover later. 

    The other mode that OracleCommand can operate in is for executing a stored procedure. This process works somewhat differently than when executing a straight text command.

    Everything is the same as above until we set the command text string. In this case, the CommandText attribute is set to the name of the stored procedure. In the following case, this is a stored procedure to get back users in a certain range of user_id’s, called GetUsers. Notice also how the following code sets the CommandType attribute to StoredProcedure:

     cmd.CommandText = “GetUsers”;
     cmd.CommandType = CommandType.StoredProcedure;

    After setting the stored procedure name and command type, we must add parameters to the OracleCommand object. This involves creating multiple OracleParameter objects and adding them to the OracleCommand’s Parameter member. This is done in the following manner:
     
    cmd.Parameters.add(new OracleParameter(“start_user”,OracleDbType.Int, 202));

    This code creates a parameter to the stored procedure GetUsers called start_user which is of type Int and has the value 202.  This object is then added to the OracleCommand’s Parameters collection that stores all of the different parameters to the stored procedure. There are several things to note here.  First of all, when creating multiple parameters, the number of parameters must match the number of parameters expected by the stored procedure.  It is also a good idea to match up order so that parameter objects are added to the Parameters collection in the same order that they appear in the stored procedure definition to make debugging easier.

    After adding the second parameter, ExecuteReader is called to get the results from the database.

     cmd.Parameters.add(new OracleParameter(“end_user”, OracleDbType.Int, 210));
     OracleDataReader reader = cmd.ExecuteReader();

    This has shown the basics of reading data out of the database.  The next step is to take that data and turn it into a .NET DataSet that can be bound to a data driven control or iterated over and changed to later update the database.

    More .NET Articles
    More By Michael Swannson


       · Windows app it work and web app generate error at conn.open() I get error...
     

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT