The Full SetBy Christian Shay
Use Microsoft Visual Studio for the complete .NET and Oracle Database development lifecycle.
Building a .NET application that accesses Oracle Database involves a variety of tasks besides writing .NET code: creating users and roles, creating tables and table data, generating SQL scripts and checking them into source control, performance tuning, and more. Those of you who use Microsoft Visual Studio will be happy to know that with Oracle Developer Tools for Visual Studio, you can accomplish almost everything you need in this development lifecycle without having to leave Visual Studio to use another tool.
In this article, I’ll build a .NET application with Oracle Database and take you step by step through the development lifecycle. Along the way, you will learn how to
I’ll also touch on other aspects of the development lifecycle, including PL/SQL debugging and performance tuning.
To follow along with the steps in this article, you will need the following:
To connect to Oracle Database from Visual Studio, create a connection in Server Explorer. (If Server Explorer is not yet visible, select View -> Server Explorer from the main menu.) Now create the new connection in Data Connections , the top-level node of Server Explorer. Right-click Data Connections , and select Add Connection . In the dialog box that appears, confirm that Oracle Database (Oracle ODP.NET) appears in the Data source field. If you do not see Oracle Database (Oracle ODP.NET) , click the Change button and select Oracle Data Provider for .NET from the list of providers. Select the database you want to connect to from the Datasource name list. Click Use a specific user name and password . In the User name field, enter SYS . In the Password field, enter the password for SYS, and ensure that Role is set to SYSDBA . Click Test connection to verify that you can connect, and click OK to complete the connection setup.
Creating a New User with User Designer
Now create an Oracle Database user that you will use during development. In Server Explorer, expand the new SYS connection you just created. You should see two child nodes: Schemas and Roles . Right-click Schemas , and choose New User from the context menu. This will launch User Designer.
I’m creating a user named CSHAY, so in User Designer, for User name , I enter CSHAY (see Figure 1). For Profile I leave the “DEFAULT” value, and for Authentication I select Password . For Password and Confirm password , I enter my chosen password. In the Tablespaces section, I select the location of the default and temporary tablespaces for the CSHAY user. When I installed Oracle Database earlier, I let the Oracle installer create a database for me, so for the default tablespace, I select the USERS tablespace, and for the temporary tablespace, I choose TEMP . Finally, I click Save , which will create my new user account. The SQL that creates the new user account will appear in the output window in Visual Studio.
Granting Privileges to the New User Account
Before you can do anything with this new user, you will need to provide some basic privileges. Specifically, you will grant the CONNECT and RESOURCE roles. To do this in Server Explorer, first right-click the SYS connection node and choose Privileges from the context menu. In the Grant/Revoke Privileges dialog box that appears, set Object Type to Role . Select User , and then select CSHAY from the list of users. At the bottom of the dialog box, there will be a list of roles. Select both the CONNECT and the RESOURCE roles, and then click Apply .
In Server Explorer, again right-click the SYS connection node and choose Privileges to reopen the Grant/Revoke Privileges dialog box. This time set Object Type to System Privileges . Select User , and then select CSHAY from the list of users. At the bottom of the dialog box will be a list of privileges. Select the CREATE ROLE privilege, and click Apply . Finally, click OK to dismiss the dialog box.
Now connect to Oracle Database, using the new CSHAY user in Server Explorer. Right-click Data Connections , and select Add Connection . Follow the same steps to connect as CSHAY that you used to connect as SYS , except set Role to Default .
Importing Tables and Table Data
Now you need some tables and table data for the application to use. There are several ways you can achieve this. If you already have a SQL script, you can run it by choosing Tools -> Run SQL Plus Script from the main menu. Or you can create the tables and table data from scratch, by using the table designer and the data window. Additionally, you can use the Import Table Wizard to import tables and table data from any datasource listed in Server Explorer that is using a data provider for .NET that supports ADO.NET 2.0. This could include any other Oracle Database instance, a Microsoft SQL Server database, Microsoft Access, a Microsoft Excel spreadsheet, or many other datasources.
For this article, you will import data from two tables contained within the EMP_DEPT.xls spreadsheet. Download this data via the link in the Next Steps box at the end of this article.
First, connect to the EMP_DEPT.xls spreadsheet in Server Explorer. Right-click Data Connections and select Add Connection . In the dialog box that appears, click the Change button and select <other> from the list. In the Data Provider list, select .NET Framework Data Provider for OLEDB and then click OK . In Server Explorer, right-click Data Connections and select Add Connection , and in the dialog box, under OLEDB provider, select Microsoft Jet 4.0 OLEDB Provider . (Note that if you wanted to use the new Excel 12.0 .xlsx spreadsheet format, you would need to select Microsoft Office 12.0 Access Database Engine OLEDB Provider .) In the Server or File name field, enter the full path to EMP_DEPT.xls, and click the Advanced button, which will open a property sheet. For the Extended Properties property, enter Excel 8.0;HDR=YES . (If you were using the Excel 12.0 .xlsx format, you would enter Excel 12.0;HDR=YES .) Click OK to close the property sheet, click Test to test the connection, and click OK once more to close the Add Connection dialog box.
Now import the data from the connected spreadsheet. Right-click the CSHAY connection node, and select Import Table . This will start the Import Table Wizard. Click Next until you see the Choose Data Source Connection screen. For the data connection source, choose the emp_dept.xls connection. Click Next to go to the Select Tables to Import screen. Under Source Table, click the check boxes next to both DEPARTMENTS$ and EMPLOYEES$ . Click each destination table name, and then edit each name to remove the dollar sign at the end (see Figure 2). The Import Data check box is checked by default, which is what you want.
Click Next to go to the Customize Columns Settings screen (see Figure 3). For each column of type BINARY_DOUBLE, highlight the row, and in the Data Type list, choose NUMBER . For EMPLOYEES.EMPLOYEE_ID, uncheck the Allows NULL check box and then check the Primary Key check box. Do the same for DEPARTMENTS .DEPARTMENT_ID. (Note that you will need to use the Select a Table list at the top of the screen to switch between the two tables.) Click Finish to import the data.
After the data has been successfully imported, click OK and then go to Server Explorer, right-click the CSHAY connection, and select Refresh . Navigate to the Tables node to view the newly imported tables.
Automatically Generating the .NET Code for a Web Application
Now, taking advantage of Visual Studio’s automatic .NET code generation features, create an ASP.NET Web application to display the data you just imported into Oracle Database. You could just as easily create a client/server Winform application or a Microsoft Office application by using similar steps.
From the Visual Studio main menu, select File -> New Project . For Visual C# Project Type select Web , and then select the ASP.NET Web Application template. Click the Design button, below the HTML code window. If the toolbox is not already visible, choose View -> Toolbox from the main menu.
Scroll down to the Data section of the toolbox, and drag and drop a GridView control onto the design surface. The GridView Tasks window should immediately open, but if it does not, click the small > icon to open it. From the Choose Data Source list, select :<New Data Source> . This will open the Data Source Configuration Wizard to the Choose a Data Source Type screen. Select the Database icon, and then click OK . On the Choose Your Data Connection screen (the next screen), select CSHAY for the data connection, and click Next . On the Save a Connection String to the Application Configuration File screen, accept the defaults and click Next .
On the Configure the Select Statement screen, select Specify a Custom SQL Statement and click Next . On the Define Custom Statements screen, click the Query Builder button. When Microsoft Query Builder launches and the Add Table dialog box appears, select the DEPARTMENTS and EMPLOYEES tables and click the Add button. In the top section of Query Builder, you will see both tables automatically joined by DEPARTMENT_ID (see Figure 4).
Now you need to decide which columns to select in your query. In the EMPLOYEES table, select the EMPLOYEE_ID , FIRST_NAME, LAST_NAME , and SALARY columns. In the DEPARTMENTS table, select the DEPARTMENT_NAME column. In the second pane from the top, order the column list by dragging and dropping so that EMPLOYEE_ID is the first Select list item and DEPARTMENT_NAME is last. Click the Execute Query button to test the query. Click OK to finish and return to the Define Custom Statements screen of the Data Source Configuration Wizard. Click Next ; test the query again (if you wish), by clicking the Test Query button; and then click Finish .
From the main menu, select Debug -> Start Without Debugging , which will open your Web browser and display your running application (see Figure 5).
Creating an Oracle Database Project
You will want to store the SQL scripts for the Oracle Database schema objects the application uses and eventually check them into source control along with the application. This will allow other developers who obtain the application code to also check out and run the SQL scripts to create the Oracle schema objects the application requires.
Before you store the SQL and application code, create a project location for the code. To add an Oracle Database project to the Visual Studio solution, first right-click the topmost node in Solution Explorer and select Add -> New Project from the context menu. Under Project Types, select Other Project Types and then Database , and then select the Oracle Database Project template. When asked to select a database reference to be stored with the project, choose the CSHAY connection and click OK .
Creating a Role for the Application
You may want to create a role that represents the privileges required to access the database objects the application uses. Using Oracle roles is a good way to ensure that application users have the correct privileges. Also, if your application later requires additional privileges, modifying a single role is much easier than making new privilege grants to each individual user. This article’s sample application, for example, will need access to the EMPLOYEES and DEPARTMENTS tables.
To create the application user role, right-click the Roles node under the CSHAY connection, and select New Role from the context menu. Enter EMPDEPT_WEBAPP in the Role Name field, and then click Save .
Next, grant privileges to the role you just created. Right-click the EMPDEPT_WEBAPP node under the Roles node, and select Privileges from the context menu. When the Privilege Wizard launches, set Object Type to Table (see Figure 6). The Schema field should be set to CSHAY . Select Grant/Revoke privileges to a user/role on one or more database object(s) , and then in the Object Name(s) list, select both DEPARTMENTS and EMPLOYEES (by holding down the Shift key while you click). Select Role , and then select EMPDEPT_WEBAPP from the list of roles.
At the bottom of the dialog box, there will be a list of privileges. Select the DELETE , INSERT , SELECT , and UPDATE privileges. Click the Preview SQL button, and then click Add to Project . This will create a SQL script that contains the GRANT statements and will add it to the Oracle Database project. (You will use these GRANT statements in the next section.) Click OK to dismiss the Preview SQL window, and then click the Apply button to execute the SQL needed to grant the privileges. Then click OK to dismiss the dialog box.
Generating SQL Scripts
Now generate the SQL scripts for the tables the application uses, so that you can check them into source control along with the application. In Server Explorer, under the CSHAY node, while holding down the Control key, select the DEPARTMENTS table, the EMPLOYEES table, and the EMPDEPT_WEBAPP role. With all three selected, right-click and select Generate Create Script to Project. Provide a script name, and click OK . After the script is added, the Oracle SQL script editor will open and display the script (see Figure 7).
Under Solution Explorer, in the Security -> Permissions folder, open the script containing GRANT statements that you created earlier. Copy these GRANT statements, and paste them at the end of the just-generated SQL script. Save this script. You can now delete the GRANT script from the Permissions folder. Although it is not necessary to merge these two scripts, it is generally simpler to use one deployment SQL script for each application in source control.
Adding Application and SQL Scripts into Source Control
The first time you use source control in Visual Studio, you will need to enable it. From the main menu, select Tools -> Options , and then select Source Control . Under Current Source Control Plug-in , select the plug-in corresponding to your source control system. If you don’t see it listed here, you will need to obtain it from the vendor and install it.
To add your solution into source control, right-click the solution name in Solution Explorer and select Add Solution to Source Control . After the solution is added to source control, new icons will appear next to the filenames and source control menu items such as Check Out for Edit will be available (see Figure 7). When users check out SQL scripts from source control, they will be able to run them (using a built-in SQL*Plus execution engine) by right-clicking and selecting Run from the context menu. This will ensure that their database schema matches the one required by the application.
Debugging Your Application’s PL/SQL
Although this application doesn’t use PL/SQL, note that you can use all the powerful features of the Visual Studio debugger with your stored functions or procedures. Debugging the PL/SQL your application uses is a very important part of the application lifecycle that was covered extensively in an earlier Oracle Magazine article (“Debugging PL/SQL from .NET”).
Now you are ready to tune the application and its use of Oracle Database. The main performance tuning feature provided by Oracle Developer Tools for Visual Studio is Oracle Performance Analyzer. You can launch it by clicking a SYSDBA connection in Server Explorer and choosing Oracle Performance Analyzer from the context menu. It monitors the use of the database under load and provides performance-improving recommendations, some of which can be implemented automatically. I’ll be deep-diving into Oracle Performance Analyzer and performance tuning tricks in an upcoming article.
Christian Shay (email@example.com) is a principal product manager at Oracle.