Oracle® Database Express Edition
2 Day Plus .NET Developer Guide
10g Release 2 (10.2)
B25312-01
February 2006
Oracle Database Express Edition 2 Day Plus .NET Developer Guide, 10g Release 2 (10.2)
B25312-01
Copyright © 2006, Oracle. All rights reserved.
Primary Author: Roza Leyderman
Contributing Authors: John Paul Cook, Mark Williams
Contributors: Alex Keh, Christian Shay
The Programs (which include both the software and documentation) contain proprietary information; they
are provided under a license agreement containing restrictions on use and disclosure and are also protected
by copyright, patent, and other intellectual and industrial property laws. Reverse engineering, disassembly,
or decompilation of the Programs, except to the extent required to obtain interoperability with other
independently created software or as specified by law, is prohibited.
The information contained in this document is subject to change without notice. If you find any problems in
the documentation, please report them to us in writing. This document is not warranted to be error-free.
Except as may be expressly permitted in your license agreement for these Programs, no part of these
Programs may be reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose.
If the Programs are delivered to the United States Government or anyone licensing or using the Programs on
behalf of the United States Government, the following notice is applicable:
U.S. GOVERNMENT RIGHTS Programs, software, databases, and related documentation and technical data
delivered to U.S. Government customers are "commercial computer software" or "commercial technical data"
pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As
such, use, duplication, disclosure, modification, and adaptation of the Programs, including documentation
and technical data, shall be subject to the licensing restrictions set forth in the applicable Oracle license
agreement, and, to the extent applicable, the additional rights set forth in FAR 52.227-19, Commercial
Computer Software—Restricted Rights (June 1987). Oracle Corporation, 500 Oracle Parkway, Redwood City,
CA 94065
The Programs are not intended for use in any nuclear, aviation, mass transit, medical, or other inherently
dangerous applications. It shall be the licensee's responsibility to take all appropriate fail-safe, backup,
redundancy and other measures to ensure the safe use of such applications if the Programs are used for such
purposes, and we disclaim liability for any damages caused by such use of the Programs.
Oracle, JD Edwards, PeopleSoft, and Retek are registered trademarks of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective owners.
The Programs may provide links to Web sites and access to content, products, and services from third
parties. Oracle is not responsible for the availability of, or any content provided on, third-party Web sites.
You bear all risks associated with the use of such content. If you choose to purchase any products or services
from a third party, the relationship is directly between you and the third party. Oracle is not responsible for:
(a) the quality of third-party products or services; or (b) fulfilling any of the terms of the agreement with the
third party, including delivery of products or services and warranty obligations related to purchased
products or services. Oracle is not responsible for any loss or damage of any sort that you may incur from
dealing with any third party.
iii
Contents
List of Examples
Preface ................................................................................................................................................................ vii
Audience...................................................................................................................................................... vii
Documentation Accessibility.................................................................................................................... vii
Related Documents .................................................................................................................................... vii
Conventions ............................................................................................................................................... viii
1 Introduction
What is Microsoft .NET Framework..................................................................................................... 1-1
Introduction to Oracle Data Provider for .NET .................................................................................. 1-2
Introduction to Oracle Developer Tools for Visual Studio .NET................................................... 1-2
Introduction to .NET Stored Procedures.............................................................................................. 1-2
2 Installing Oracle Developer Tools
What You Need......................................................................................................................................... 2-1
Installing Oracle Developer Tools........................................................................................................ 2-2
Unlocking the User Account .................................................................................................................. 2-4
Uninstalling Oracle Developer Tools .................................................................................................. 2-4
3 Connecting to the Database
Starting a New Project............................................................................................................................. 3-1
Adding a Reference.................................................................................................................................. 3-2
Adding Initial Programmatic Statements............................................................................................ 3-4
Adding Connection Elements................................................................................................................ 3-5
Using the Connection Object.............................................................................................................. 3-11
Running the Application ..................................................................................................................... 3-14
Error Handling....................................................................................................................................... 3-14
Closing the Database Connection...................................................................................................... 3-18
4 Building an Oracle Data Provider for .NET Application
Copying a Project ..................................................................................................................................... 4-1
Using the Command Object................................................................................................................... 4-3
Retrieving Data: a Simple Query .......................................................................................................... 4-4
Retrieving Data: Bind Variables ........................................................................................................... 4-5
iv
Retrieving Data: Multiple Values ......................................................................................................... 4-7
Using the DataSet Class with Oracle Data Provider for .NET ........................................................ 4-8
Inserting, Deleting and Updating Data ............................................................................................ 4-13
5 Using Oracle Developer Tools for Visual Studio .NET
Connecting to the Oracle Database....................................................................................................... 5-1
Creating a Table and Its Columns ........................................................................................................ 5-3
Creating a Table Index ............................................................................................................................ 5-5
Adding Table Constraints ...................................................................................................................... 5-6
Adding Data to a Table ........................................................................................................................... 5-8
Generating Code Automatically............................................................................................................ 5-9
Enabling Database Updates ................................................................................................................ 5-13
6 Using PL/SQL Stored Procedures and REF Cursors
Introduction to PL/SQL Packages and Package Bodies.................................................................... 6-1
Introduction to PL/SQL Stored Procedures......................................................................................... 6-1
Introduction to Ref Cursors.................................................................................................................... 6-1
Creating a PL/SQL Stored Procedure that Uses Ref Cursors........................................................... 6-2
Running a PL/SQL Stored Procedure Using Oracle Data Provider for .NET............................... 6-8
7 Deploying .NET Stored Procedures
Starting the Common Language Runtime Service ............................................................................ 7-1
Creating an Oracle Project ...................................................................................................................... 7-2
Creating a New Connection ................................................................................................................... 7-3
Creating .NET Stored Functions and Procedures............................................................................... 7-5
Deploying .NET Stored Functions and Procedures........................................................................... 7-7
Running .NET Stored Functions and Procedures ........................................................................... 7-11
8 Including Globalization Support
Introduction to Global Applications .................................................................................................... 8-1
Developing Global Applications with the .NET Framework.......................................................... 8-1
Presenting Data in the Correct User Local Convention .................................................................... 8-2
Oracle Date Formats .......................................................................................................................... 8-2
Oracle Number Formats.................................................................................................................... 8-3
Oracle Linguistic Sorts....................................................................................................................... 8-4
Oracle Error Messages....................................................................................................................... 8-5
Synchronizing the .NET and Oracle Database Locale Environments ........................................... 8-6
Client Globalization Support in Oracle Data Provider for .NET ................................................... 8-7
Client Globalization Settings............................................................................................................ 8-7
Session Globalization Settings.......................................................................................................... 8-8
Thread-Based Globalization Settings........................................................................................... 8-12
Index
v
List of Examples
3–1 Adding Initial Programmatic Statements: C#......................................................................... 3-5
3–2 Adding Initial Programmatic Statements: VB ........................................................................ 3-5
3–3 Easy Connect Naming Method Syntax for Data Source .................................................... 3-12
3–4 Creating an OracleConnection Object: C#............................................................................ 3-12
3–5 Creating an OracleConnection Object: VB ........................................................................... 3-12
3–6 Building and Opening a Connection: C#.............................................................................. 3-13
3–7 Building and Opening a Connection: VB............................................................................. 3-13
3–8 Disabling the Connect Button: C# ......................................................................................... 3-13
3–9 Disabling the Connect Button: VB......................................................................................... 3-14
3–10 Error Handling with Try-Catch-Finally Syntax: C#............................................................ 3-15
3–11 Error Handling with Try-Catch-Finally Syntax: VB ........................................................... 3-15
3–12 Catching Common Database Error Messages: C#............................................................... 3-16
3–13 Catching Common Database Error Messages: VB.............................................................. 3-16
3–14 Closing and Disposing a Connection: C#............................................................................. 3-18
3–15 Closing and Disposing a Connection: VB ............................................................................ 3-18
3–16 Closing and Disposing a Connection when Out of Scope: C# .......................................... 3-18
4–1 Creating a SQL Statement String: C# ....................................................................................... 4-3
4–2 Creating a SQL Statement String: VB....................................................................................... 4-3
4–3 Using a Command to Query the Database: C#....................................................................... 4-3
4–4 Using a Command to Query the Database: VB ...................................................................... 4-3
4–5 Starting the OracleDataReader: C#........................................................................................... 4-4
4–6 Starting the OracleDataReader: VB.......................................................................................... 4-4
4–7 Retrieving a Value: C#................................................................................................................ 4-5
4–8 Retrieving a Value: VB ............................................................................................................... 4-5
4–9 SELECT Statement without Bind Variables............................................................................ 4-6
4–10 SELECT Statement with Bind Variables.................................................................................. 4-6
4–11 Using a Bind Variable: C#.......................................................................................................... 4-6
4–12 Using a Bind Variable: VB ......................................................................................................... 4-7
4–13 UPDATE Statement with Bind Variables................................................................................ 4-7
4–14 Querying for a Multiple Column Multiple Row Result........................................................ 4-8
4–15 Looping Through a Multi-Row Query Result: C# ................................................................. 4-8
4–16 Looping Through a Multi-Row Query Result: VB................................................................. 4-8
4–17 Using DataSet Class: Declaring Variables: C# ..................................................................... 4-10
4–18 Using DataSet Class: Declaring Variables: VB..................................................................... 4-10
4–19 Disabling the Save Button: C#................................................................................................ 4-11
4–20 Disabling the Save Button: VB ............................................................................................... 4-11
4–21 Binding Data to the Grid: C#.................................................................................................. 4-11
4–22 Binding Data to the Grid: VB ................................................................................................. 4-11
4–23 Updating DataSet: C#.............................................................................................................. 4-12
4–24 Updating DataSet: VB ............................................................................................................. 4-12
5–1 Generated SQL Form of the New Table .................................................................................. 5-5
5–2 Creating a Table Index in SQL.................................................................................................. 5-6
5–3 Adding Foreign Key and Primary Key Constraints to a Table............................................ 5-8
5–4 Filling Data into the Form: C#................................................................................................ 5-12
5–5 Filling Data into the Form: VB ............................................................................................... 5-12
5–6 The save_Click() Method: C# ................................................................................................. 5-13
5–7 The save_Click() Method: VB................................................................................................. 5-14
6–1 PL/SQL Code for Package HR_DATA.................................................................................... 6-4
6–2 Assigning Reference Cursors .................................................................................................... 6-5
6–3 Changing OracleCommand to Use a Stored Procedure: C#................................................. 6-8
6–4 Changing OracleCommand to Use a Stored Procedure: VB ................................................ 6-8
6–5 Defining and Binding OracleParameter Objects for Stored Procedure: C#........................ 6-8
6–6 Defining and Binding OracleParameter Objects for Stored Procedure: VB....................... 6-9
7–1 Adding getDepartmentno() Method Code: C#....................................................................... 7-5
评论3