Skip Headers
Oracle® Database Express Edition 2 Day Plus .NET Developer Guide
10g Release 2 (10.2)

Part Number B25312-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

7 Deploying .NET Stored Procedures

This chapter discusses how to use and deploy .NET stored procedures in your application. You can use custom stored procedures in your ODP.NET code in the same manner as any other stored procedure.

This chapter contains the following sections:

Starting the Common Language Runtime Service

To use .NET Stored Procedures, you must first start the XE Common Language Runtime agent, represented by the OracleXEClrAgent service. This service may not start by default. Note that it is located on the Oracle XE database server, not on the client.

  1. From the Start menu, select All Programs, then select Administrative Tools, and finally, select Services.

    Description of services01.gif follows
    Description of the illustration services01.gif

  2. In the Services window, click the Extended tab.

    Scroll down the list of Services, and select OracleXEClrAgent.

    Click the Start hyperlink.

    Description of services02.gif follows
    Description of the illustration services02.gif

  3. The Service Control window shows that the OracleXEClrAgent is starting.

    Description of services03.gif follows
    Description of the illustration services03.gif

  4. When the Service Control window closes, note that the status of the OracleXEClrAgent is changed to Started.

    Description of services04.gif follows
    Description of the illustration services04.gif

Creating an Oracle Project

Follow these steps to start a new Oracle project in Visual Studio .NET 2003:

  1. From the File menu, select New, and then select Project.

    A New Project dialog box appears.

  2. On the left side of the New Project dialog box under Project Types, select Visual C# Projects.

    On the right side of the New Project dialog box under Templates, select Oracle Project.

    For Name, enter HR_DeployStored_CS.

    For Location, enter C:\HR\Visual Studio Projects.

    Click OK.

    If you wish to create project in Visual Basic, under Project Types select Visual Basic Projects instead, and enter HR_DeployStored_VB under Name.

    Description of deploy01.gif follows
    Description of the illustration deploy01.gif

Creating a New Connection

  1. In Oracle Explorer, right-click Data Connections. From the menu, select Add Connection.

    Description of deploy02.gif follows
    Description of the illustration deploy02.gif

    An Add Connection window appears.

  2. In the Add Connection window, enter the following information:

    Data source name: Use the Local Database if you are connecting to a database on the same machine. Otherwise, use the alias of the remote database instance.

    Select the Use a specific user name and password option.

    For User name, enter hr.

    For Password, enter hr, or the password that was set when unlocking and setting up the hr account.

    To save the password for future sessions, check the Save password box. While it is not advisable to save connection password within your application in clear text for security reasons, we will do it in this demonstration.

    Ensure that the Role is set to Default. This refers to the default roles that have been granted to the user hr.

    The Connection name should be generated automatically from the Data source name and the User name values.

    Click the Apply Filters tab and check that the HR schema is in the Displayed schemas column. Only the schema objects (tables, views, and so on) from the schemas selected in the Apply Filters tab are displayed when you expand the schema category nodes in the data connection.

    Description of deploy03.gif follows
    Description of the illustration deploy03.gif

    Click the Connection Details tab, and click Test connection.

    Description of odt03.gif follows
    Description of the illustration odt03.gif

    The test should succeed. Click OK.

    If the test fails, it may be due to one or more of the following issues that you must address before proceeding with further steps:

    • The database is not started.

    • The database connectivity is not properly configured.

    • You do not have the correct user name, password, and role.

  3. Click OK to close the Add Connection window.

    The Oracle Explorer window should now contain the hr.(Local Database) connection.

Creating .NET Stored Functions and Procedures

  1. Select Class1.cs tab in your project.

  2. Paste the getDepartmentno() method into the Class1 declaration, as shown in Example 7-1 and Example 7-2.

    Example 7-1 Adding getDepartmentno() Method Code: C#

    public static int getDepartmentno(int employee_id)
    {
      int department_id = 0;
    
      // Get a connection to the db
      OracleConnection conn = new OracleConnection();
      conn.ConnectionString = "context connection=true";
      conn.Open();
    
      // Create and execute a command
      OracleCommand cmd = conn.CreateCommand();
      cmd.CommandText = "select department_id from employees where employee_id = :1";
      cmd.Parameters.Add(":1", OracleDbType.Int32, employee_id,
      ParameterDirection.Input);
      OracleDataReader rdr = cmd.ExecuteReader();
    
      while(rdr.Read())
        department_id=rdr.GetInt32(0);
    
      rdr.Close();
      cmd.Dispose();
    
      // Return the employee's department number
      return department_id;
    }
    

    Example 7-2 Adding getDepartmentno() Method Code: VB

    Public Shared Function getDepartmentno(ByVal employee_id As Integer) As Integer
      Dim department_id As Integer = 0 
    
      ' Get a connection to the db
      Dim conn As OracleConnection = New OracleConnection
      conn.ConnectionString = "context connection=true"
      conn.Open()
    
      ' Create and execute a command
      Dim cmd As OracleCommand = conn.CreateCommand()
      cmd.CommandText = "select department_id from employees where employee_id = :1"
      cmd.Parameters.Add(":1", OracleDbType.Int32, employee_id,     ParameterDirection.Input)
      Dim rdr As OracleDataReader = cmd.ExecuteReader()
    
      While rdr.Read()
        department_id = rdr.GetInt32(0)
    
      End While
    
      rdr.Close()
      cmd.Dispose()
    
      ' Return the employee's department number
      getDepartmentno = department_id
    
    End Function
    
    Description of deploy04.gif follows
    Description of the illustration deploy04.gif

  3. Using the Ctrl+S keyboard shortcut, save Class1.

  4. From the Build menu, select Build Solution.

    Description of deploy05.gif follows
    Description of the illustration deploy05.gif

  5. An Output window shows that the build was successful. Close the Output window.

Deploying .NET Stored Functions and Procedures

Follow these steps to deploy a .NET Stored Procedure:

  1. From the Build menu, select Deploy Solution.

    Description of deploy06.gif follows
    Description of the illustration deploy06.gif

    An Oracle Deployment Wizard for .NET window appears.

  2. In the Oracle Deployment Wizard for .NET window, click Next.

    Description of deploy07.gif follows
    Description of the illustration deploy07.gif

  3. On the Configure Your Connection window, click New Connection.

    Description of deploy08.gif follows
    Description of the illustration deploy08.gif

  4. You must now establish a connection with SYSDBA privileges.

    • For the Data Source Name, use (Local Database).

    • For Username, enter sys.

    • For Password, enter the current sys password.

    • Click Test Connection.

    To use the Oracle Application Express to set the sys account password, see Chapter 6, "Managing Users and Security" in the Oracle Database Express Edition 2 Day DBA.

    Description of deploy09.gif follows
    Description of the illustration deploy09.gif

    The test result window appears.

  5. Click OK to close the test result window.

  6. In the Add Connection window, click OK.

  7. In the Oracle Deployment Wizard for .NET window, click Next.

  8. On the Specify your deployment option window, ensure that Copy assembly and generate stored procedures is selected, and click Next.

    Description of deploy10.gif follows
    Description of the illustration deploy10.gif

  9. On the Specify an assembly and library name window, accept the defaults and click Next.

    Description of deploy11.gif follows
    Description of the illustration deploy11.gif

  10. On the Specify copy options window, accept the defaults and click Next.

    Description of deploy12.gif follows
    Description of the illustration deploy12.gif

  11. On the Specify methods and security details window, under Available methods, expand HR_DeployStored_CS (or HR_DeployStored_VB), then expand Class1, and select the getDepartmentno() method.

    Under Method Details, select HR from the Schema drop-down list.

    Click Next.

    Description of deploy13.gif follows
    Description of the illustration deploy13.gif

  12. On the Summary window, click Finish.

    Description of deploy14.gif follows
    Description of the illustration deploy14.gif

Running .NET Stored Functions and Procedures

Follow these steps to run the .NET Stored procedure you created in Section "Deploying .NET Stored Functions and Procedures":

  1. In Oracle Explorer, expand the hr.(LocalDatabase) connection. Expand Functions. Right-click GETDEPARTMENTNO and select Run.

    Description of deploy15.gif follows
    Description of the illustration deploy15.gif

    The Run Function window appears.

  2. In the Run Function window, enter a Value of 100 for EMPLOYEE_ID.

    Click OK.

    Description of deploy16.gif follows
    Description of the illustration deploy16.gif

  3. Note that the return value for department is 90.

    Description of deploy17.gif follows
    Description of the illustration deploy17.gif