<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>All Free Tech &#187; visual studio</title>
	<atom:link href="http://www.allfreetech.com/tag/visual-studio/feed" rel="self" type="application/rss+xml" />
	<link>http://www.allfreetech.com</link>
	<description>For developers</description>
	<lastBuildDate>Tue, 01 Feb 2011 13:45:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Build a .NET Application on the Oracle Database with Visual Studio 2005 or 2008</title>
		<link>http://www.allfreetech.com/microsoft-net/build-a-net-application-on-the-oracle-database-with-visual-studio-2005-or-2008-115.html</link>
		<comments>http://www.allfreetech.com/microsoft-net/build-a-net-application-on-the-oracle-database-with-visual-studio-2005-or-2008-115.html#comments</comments>
		<pubDate>Wed, 20 Jan 2010 07:31:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Microsoft .Net]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://www.allfreetech.com/?p=115</guid>
		<description><![CDATA[With the popularity of Microsoft&#39;s .NET Framework, many developers are hungry for information about the best means of integrating .NET applications with Oracle&#8212;not only in terms of basic connectivity, but also in relationship to effective and efficient application development using Visual Studio 2005 or 2008. In this article, I&#39;ll explain the basic yet essential processes [...]]]></description>
			<content:encoded><![CDATA[<p>With the popularity of Microsoft&#39;s .NET Framework, many developers are hungry for information about the best means of integrating .NET applications with Oracle&mdash;not only in terms of basic connectivity, but also in relationship to effective and efficient application development using Visual Studio 2005 or 2008.</p>
<p>In this article, I&#39;ll explain the basic yet essential processes involved in building a .NET application that uses an Oracle database, including:</p>
<ul type="square">
<li>How to add project references to support Oracle class libraries in your .NET project</li>
<li>How to create Oracle Database connection strings</li>
<li>How to work with Connection, Command, and DataReader objects</li>
</ul>
<p>You will have the opportunity to apply what you have learned in three <a class="bodylink" href="http://www.oracle.com/technology/pub/articles/cook-vs08.html#lab1">practice labs</a>, ranging in difficulty from the relatively simple to the more complex. The article&#39;s screenshots are taken from Visual Studio 2008, but the experience is very similar in Visual Studio 2005.</p>
<p>For information and labs about how to secure your application, see my article &quot;<a class="bodylink" href="http://www.oracle.com/technology/pub/articles/mastering_dotnet_oracle/cook_masteringdotnet.html">Securing a .NET Application on the Oracle Database</a>&quot;. (Also, see the OTN <a class="bodylink" href="http://www.oracle.com/technology/tech/dotnet/index.html">.NET Developer Center</a> for technical articles covering a range of Oracle.NET application lifecycle issues.)</p>
<p>Note that the free <a class="bodylink" href="http://www.oracle.com/technology/tech/dotnet/tools/index.html">Oracle Developer Tools for Visual Studio</a>, available for <a class="bodylink" href="http://www.oracle.com/technology/tech/dotnet/tools/index.html">download</a> from OTN, provides a Visual Studio add-in that makes the development of .NET apps on Oracle much easier and more intuitive. That subject is beyond our scope here, however.</p>
<h2>.NET Data Provider</h2>
<p>In addition to basic Oracle client connectivity software, .NET applications require the use of what is known as a <em>managed data provider</em> (where &quot;managed&quot; refers to code managed by the .NET framework). The data provider is the layer between the .NET application code and the Oracle client connectivity software. In almost every case, the best performance is achieved by using a provider optimized for a specific database platform instead of the generic .NET OLE DB data provider.</p>
<p>Oracle, Microsoft, and third-party vendors all offer .NET data providers optimized for Oracle. Oracle and Microsoft make their Oracle data providers available for free. (Microsoft&#39;s provider for the .NET Framework 2.0 is included in the framework, but it still requires Oracle client software installation.) In this article, we will use of the Oracle Data Provider for .NET (ODP.NET), which is included with the Oracle Database or as a separate <a class="bodylink" href="http://www.oracle.com/technology/software/tech/windows/odpnet/index.html">download</a>.</p>
<p>ODP.NET provides standard ADO.NET data access, while exposing Oracle database-specific features, such as XML DB, data access performance optimizations, and Real Application Clusters connection pooling.</p>
<p>When ODP.NET and Oracle client software are installed, application development using Visual Studio can begin. It&#39;s a good idea to confirm client connectivity before starting development. If you can connect to Oracle using Oracle client software such as SQL*Plus on the same machine as Visual Studio, then you know that your Oracle client-side software is properly installed and configured.</p>
<p>If you are new to Oracle, see the section &quot;Installing .NET Products&quot; in the <a class="bodylink" href="http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28843/toc.htm"><em>Oracle Database 2 Day Developer&#39;s Guide</em></a> for background information regarding installing and configuring ODP.NET specifically, or to the <a class="bodylink" href="http://www.oracle.com/technology/documentation/database.html">Oracle Database Documentation Library</a> for general information about Oracle Database.</p>
<h2>Creating a Project in Visual Studio 2005 or 2008</h2>
<p>Let&#39;s create an ODP.NET application that retrieves data from an Oracle database. Later, we&#39;ll see how to perform error handling with ODP.NET and handle an additional data retrieval scenario.</p>
<p>After starting Visual Studio, the first task is to create a project. You can either select <strong>File | New | Project</strong> as shown below or click the <strong>New Project</strong> button located directly under <strong>File</strong>.</p>
<div align="center">
<p><img alt="Figure 1" src="http://www.oracle.com/technology/pub/images/cook-vs08-f1.gif" /><br />
		<em><strong>Figure 1</strong> Creating a new project in Visual Studio 2008 Service Pack 1</em></p>
</div>
<p>A <strong>New Project</strong> dialog box appears. On the left side of the dialog box under <strong>Project Types</strong>, select the programming language of your choice. In our example, &quot;Visual Basic&quot; was chosen. On the right side under <strong>Visual Studio installed templates</strong>, choose a project template. To keep things simple, a &quot;Windows Forms Application&quot; is selected.</p>
<div align="center">
<p><img alt="Figure 2" src="http://www.oracle.com/technology/pub/images/cook-vs08-f2.gif" /><br />
		<em><strong>Figure 2</strong> Using the <strong>New Project</strong> dialog</em></p>
</div>
<p>You&#39;ll want to specify meaningful names for the project name (we used OraWinApp) and the solution name (we used OraWinApp). A solution contains one or more projects. When a solution contains only one project, many people use the same name for both.</p>
<h2>Adding a Reference</h2>
<p>Because our project must connect to an Oracle database, it is necessary to add a reference to the ODP.NET DLL containing the data provider of our choice. Within the Solution Explorer, select the project name, right click and select <strong>Add Reference</strong>. Alternatively, you can go to the menu bar and select <strong>Project</strong> and then select <strong>Add Reference</strong>.</p>
<div align="center">
<p><img alt="Figure 3" src="http://www.oracle.com/technology/pub/images/cook-vs08-f3.gif" /><br />
		<em><strong>Figure 3</strong> Adding a reference</em></p>
</div>
<p>The <strong>Add Reference</strong> dialog box appears.</p>
<div align="center">
<p><img alt="Figure 4" src="http://www.oracle.com/technology/pub/images/cook-vs08-f4.gif" /><br />
		<strong><em>Figure 4</em></strong><em> Selecting the ODP.NET Managed Data Provider</em></p>
</div>
<p>ODP.NET is found under the Oracle.DataAccess component name. Select <strong>Oracle.DataAccess</strong> from the list, then click <strong>OK</strong> to make the ODP.NET data provider known to your project.</p>
<h2>Visual Basic/C# Statements</h2>
<p>After adding references, it is standard practice to add Visual Basic Imports statements or C# using statements. Technically, these statements are not required but they do allow you to refer to database objects without using lengthy, fully qualified names.</p>
<p>By convention, these statements appear at or near the top of a code file, before the namespace or class declaration.</p>
<pre>Imports Oracle.DataAccess.Client &#39; Visual Basic ODP.NET Oracle managed provider

using Oracle.DataAccess.Client; // C# ODP.NET Oracle managed provider
</pre>
<p>If you added the reference, Intellisense will help you complete the addition of an Imports or using statement as shown in Figure 5.</p>
<div align="center">
<p><img alt="Figure 5" src="http://www.oracle.com/technology/pub/images/cook-vs08-f5.gif" /><br />
		<strong><em>Figure 5</em></strong><em> Adding an Imports statement in Visual Basic</em></p>
</div>
<h2>Connection Strings and Objects</h2>
<p>An Oracle connection string is inseparable from Oracle names resolution. Suppose you had a database alias of OraDb defined in a tnsnames.ora file as follows:</p>
<pre>OraDb=
  (DESCRIPTION=
    (ADDRESS_LIST=
      (ADDRESS=(PROTOCOL=TCP)(HOST=ORASRVR)(PORT=1521))
    )
    (CONNECT_DATA=
      (SERVER=DEDICATED)
      (SERVICE_NAME=ORCL)
    )
  )
</pre>
<p>The OraDb alias defines the database address connection information for the client. To use the OraDb alias defined in the tnsnames.ora file shown above, you would use the following syntax:</p>
<pre>Dim oradb As String = &quot;Data Source=OraDb;User Id=scott;Password=tiger;&quot; &#39; Visual Basic

string oradb = &quot;Data Source=OraDb;User Id=scott;Password=tiger;&quot;; // C#
</pre>
<p>You can modify the connection string to obviate the need for the tnsnames.ora file, however. Simply replace the name of the alias with how it would be defined in a tnsnames.ora file.</p>
<pre>&#39; Visual Basic
Dim oradb As String = &quot;Data Source=(DESCRIPTION=&quot; _
           + &quot;(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=ORASRVR)(PORT=1521)))&quot; _
           + &quot;(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));&quot; _
           + &quot;User Id=scott;Password=tiger;&quot;

// C#
string oradb = &quot;Data Source=(DESCRIPTION=&quot;
             + &quot;(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=ORASRVR)(PORT=1521)))&quot;
             + &quot;(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));&quot;
             + &quot;User Id=scott;Password=tiger;&quot;;
</pre>
<p>As you can see above, the username and password are embedded in the connection string in clear text. This is the simplest approach to creating a connection string. However, the clear text approach is undesirable from a security perspective. In particular, you must understand that compiled .NET application code is only marginally more secure than the clear text source code files. It is very easy to decompile .NET DLL and EXE files and view the original clear text contents. (Encryption is in fact the appropriate solution, but that subject would be a quite lengthy digression from our discussion here.)</p>
<p>Next, you must instantiate a connection object from the connection class. The connection string must be associated with the connection object.</p>
<pre>Dim conn As New OracleConnection(oradb) &#39; Visual Basic

OracleConnection conn = new OracleConnection(oradb); // C#
</pre>
<p>Notice that the connection string is associated with the connection object by being passed through the object&#39;s constructor, which is overloaded. The constructor&#39;s other overload allows the following alternative syntax:</p>
<pre>Dim conn As New OracleConnection() &#39; Visual Basic
conn.ConnectionString = oradb

OracleConnection conn = new OracleConnection(); // C#
conn.ConnectionString = oradb;
</pre>
<p>After associating a connection string with a connection object, use the Open method to make the actual connection.</p>
<pre>conn.Open() &#39; Visual Basic

conn.Open(); // C#
</pre>
<p>We&#39;ll cover error handling later.</p>
<h2>Command Object</h2>
<p>The Command object is used to specify the SQL command text that is executed, either a SQL string or a stored procedure. Similar to the Connection object, it must be instantiated from its class and it has an overloaded constructor. In this sample, ODP.NET will perform a SQL query to return the department name (DNAME) from the departments table (DEPT) where the department number (DEPTNO) is 10.</p>
<pre>Dim sql As String = &quot;select dname from dept where deptno = 10&quot; &#39; Visual Basic
Dim cmd As New OracleCommand(sql, conn)
cmd.CommandType = CommandType.Text

string sql = &quot;select dname from dept where deptno = 10&quot;; // C#
OracleCommand cmd = new OracleCommand(sql, conn);
cmd.CommandType = CommandType.Text;
</pre>
<p>Using different overloads, the syntax can be structured slightly differently. The Command object has methods for executing the command text, which will be seen in the next section. Different methods are appropriate for different types of SQL commands.</p>
<h2>Retrieving a Scalar Value</h2>
<p>Retrieving data from the database can be accomplished by instantiating an OracleDataReader object and using the ExecuteReader method, which returns an OracleDataReader object. Returned data is accessible by passing either the column name or zero-based column ordinal to the OracleDataReader.</p>
<pre>Dim dr As OracleDataReader = cmd.ExecuteReader() &#39; Visual Basic
dr.Read()

Label1.Text = dr.Item(&quot;dname&quot;) &#39; retrieve by column name
Label1.Text = dr.Item(0) &#39; retrieve the first column in the select list
Label1.Text = dr.GetString(0) &#39; return a .NET data type
Label1.Text = dr.GetOracleString(0) &#39; return an Oracle data type
</pre>
<p>There are typed accessors for returning .NET native data types and others for returning native Oracle data types, all of which are available in C#, Visual Basic, or any other .NET language. Zero-based ordinals are passed to the accessors to specify which column to return.</p>
<pre>OracleDataReader dr = cmd.ExecuteReader(); // C#
dr.Read();

label1.Text = dr[&quot;dname&quot;].ToString(); // C# retrieve by column name
label1.Text = dr.GetString(0).ToString();  // return a .NET data type
label1.Text = dr.GetOracleString(0).ToString();  // return an Oracle data type
</pre>
<p>In this simplified example, the returned value of DNAME is a string and is used to set the value of the label control&#39;s text property, which is also a string. But if DEPTNO, which is not a string, had been retrieved instead, there would be a data type mismatch. The .NET runtime attempts to implicitly convert from one data type to another when the source and destination data types don&#39;t match. Sometimes the data types are incompatible and the implicit conversion fails, throwing an exception. But even when it works, it&#39;s still better to use explicit data type conversions instead of implicit data type conversion.</p>
<p>An explicit cast to integer is shown below:</p>
<pre>Label1.Text = CStr(dr.Item(&quot;deptno&quot;)) &#39; Visual Basic integer to string cast

C# is not as forgiving as Visual Basic on implicit conversions. You&#39;ll find yourself doing explicit conversions: 

label1.Text = dr.GetInt16(&quot;deptno&quot;).ToString(); // C#
</pre>
<p>You can explicitly cast scalar values as well as arrays.</p>
<h2>Close and Dispose</h2>
<p>Either the connection object&#39;s <tt>Close</tt> or the <tt>Dispose</tt> method should be called to close the connection to the database. The <tt>Dispose</tt> method calls the <tt>Close</tt> method.</p>
<pre>conn.Close()   &#39; Visual Basic
conn.Dispose() &#39; Visual Basic

conn.Close();   // C#
conn.Dispose(); // C#
</pre>
<p>You don&#39;t have to explicitly call <tt>Close</tt> or <tt>Dispose</tt> if you use VB&#39;s <tt>Using</tt> keyword or C#&#39;s <tt>using</tt> keyword.</p>
<pre>using (OracleConnection conn = new OracleConnection(oradb)) // C#
{
    conn.Open();

    OracleCommand cmd = new OracleCommand();
    cmd.Connection = conn;
    cmd.CommandText = &quot;select dname from dept where deptno = 10&quot;;
    cmd.CommandType = CommandType.Text;

    OracleDataReader dr = cmd.ExecuteReader();
    dr.Read();

    label1.Text = dr.GetString(0);
}
</pre>
<p>In addition, OracleCommand includes a Dispose method; OracleDataReader includes a <tt>Close</tt> and <tt>Dispose</tt> method. Closing and disposing .NET objects free up system resources, ensuring more efficient application performance, which is especially important under high load conditions. You can experiment with some of the concepts we&#39;ve learned here in <a class="bodylink" href="http://www.oracle.com/technology/pub/articles/cook-vs08.html#lab1">Lab 1</a> (Retrieving Data from the Database) and <a class="bodylink" href="http://www.oracle.com/technology/pub/articles/cook-vs08.html#lab2">Lab 2</a> (Adding Interactivity).</p>
<h2>Error Handling</h2>
<p>When an error occurs, .NET applications should gracefully handle the error and inform the user with a meaningful message. Try-Catch-Finally structured error handling is a part of .NET languages; here is a relatively minimalist example of using the Try-Catch-Finally syntax:</p>
<pre>&#39; Visual Basic
Try
    conn.Open()

    Dim cmd As New OracleCommand
    cmd.Connection = conn
    cmd.CommandText = &quot;select dname from dept where deptno = &quot; + TextBox1.Text
    cmd.CommandType = CommandType.Text

    If dr.Read() Then
        Label1.Text = dr.Item(&quot;dname&quot;) &#39; or use dr.Item(0)
    End If
Catch ex As Exception &#39; catches any error
    MessageBox.Show(ex.Message.ToString())
Finally
    &#39; In a real application, put cleanup code here.
End Try

// C#
try
{
    conn.Open();

    OracleCommand cmd = new OracleCommand();
    cmd.Connection = conn;
    cmd.CommandText = &quot;select dname from dept where deptno = &quot; + textBox1.Text;
    cmd.CommandType = CommandType.Text;

    if (dr.Read()) // C#
    {
        label1.Text = dr[&quot;dname&quot;].ToString();
                   // or use dr.GetOracleString(0).ToString()
    }
}
catch (Exception ex) // catches any error
{
    MessageBox.Show(ex.Message.ToString());
}
finally
{
    // In a real application, put cleanup code here.
}
</pre>
<p>Although this approach will gracefully capture any errors in attempting to get data from the database, it is not user friendly. For example, look at the following message displayed when the database is unavailable:</p>
<div align="center">
<p><img alt="Figure 6" src="http://www.oracle.com/technology/pub/images/cook-vs08-f6.gif" /><br />
		<strong><em>Figure 6</em></strong><em> An ORA-12545 error caught and displayed to the user</em></p>
</div>
<p>An ORA-12545 is quite meaningful for an Oracle DBA or developer, but not for an end user. A better solution is to add an additional <tt>Catch</tt> statement to trap for the most common database errors and provide user-friendly messages.</p>
<pre>Catch ex As OracleException &#39; catches only Oracle errors
    Select Case ex.Number
        Case 1
            MessageBox.Show(&quot;Error attempting to insert duplicate data.&quot;)
        Case 12545
            MessageBox.Show(&quot;The database is unavailable.&quot;)
        Case Else
            MessageBox.Show(&quot;Database error: &quot; + ex.Message.ToString())
    End Select
Catch ex As Exception &#39; catches any error
    MessageBox.Show(ex.Message.ToString())

catch (OracleException ex) // catches only Oracle errors
{
    switch (ex.Number)
    {
        case 1:
            MessageBox.Show(&quot;Error attempting to insert duplicate data.&quot;);
            break;
        case 12545:
            MessageBox.Show(&quot;The database is unavailable.&quot;);
            break;
        default:
            MessageBox.Show(&quot;Database error: &quot; + ex.Message.ToString());
            break;
    }
}
catch (Exception ex) // catches any error not previously caught
{
    MessageBox.Show(ex.Message.ToString());
}
</pre>
<p>Notice the two <tt>Catch</tt> statements in the code sample above. If there aren&#39;t any Oracle errors to catch, the first statement branch is skipped, leaving any other non-Oracle error to be caught by the second statement. <tt>Catch</tt> statements must be ordered in the code from most specific to most general. After implementing the user-friendly exception handling code, the ORA-12545 error message appears as follows:</p>
<div align="center">
<p><img alt="Figure 7" src="http://www.oracle.com/technology/pub/images/cook-vs08-f7.gif" /><br />
		<strong><em>Figure 7</em></strong><em> A user-friendly error message for an ORA-12545 error</em></p>
</div>
<p>The <tt>Finally</tt> code block is always executed regardless of whether or not an error occurred. It is where cleanup code belongs. If you don&#39;t use <tt>Using</tt> or <tt>using</tt>, you should dispose your connection and other objects in the <tt>Finally</tt> code block.</p>
<h2>Retrieving Multiple Values Using a DataReader</h2>
<p>So far our examples have only showed how to retrieve a single value. An OracleDataReader can retrieve values for multiple columns and multiple rows. First consider a multiple column, single row query:</p>
<pre>select deptno, dname, loc from dept where deptno = 10
</pre>
<p>To obtain the values of the columns, either zero-based ordinals or column names can be used. Ordinals are relative to the order in the query. Thus, the LOC column&#39;s value can be retrieved in Visual Basic by using either dr.Item(2) or dr.Item(&quot;loc&quot;).</p>
<p>Here is a code snippet that concatenates the DNAME and LOC columns from the previous query:</p>
<pre>Label1.Text = &quot;The &quot; + dr.Item(&quot;dname&quot;) + &quot; department is in &quot; + dr.Item(&quot;loc&quot;) &#39; VB

label1.Text = &quot;The &quot; + dr[&quot;dname&quot;].ToString() + &quot; department is in &quot; +
              dr[&quot;loc&quot;].ToString();  // C#
</pre>
<p>Now consider a query that returns multiple rows:</p>
<pre>select deptno, dname, loc from dept
</pre>
<p>To process multiple rows returned from an OracleDataReader, some type of looping construct is needed. Furthermore, a control that can display multiple rows is desirable. An OracleDataReader is a forward-only, read-only cursor, so it can&#39;t be bound to an updateable or fully scrollable control such as a Windows Forms DataGrid control. An OracleDataReader is compatible with a ListBox control, as the following code snippet illustrates:</p>
<pre>While dr.Read() &#39; Visual Basic
   ListBox1.Items.Add(&quot;The &quot; + dr.Item(&quot;dname&quot;) + &quot; department is in &quot; + dr.Item(&quot;loc&quot;))
End While

while (dr.Read()) // C#
{
    listBox1.Items.Add(&quot;The &quot; + dr[&quot;dname&quot;].ToString() + &quot; department is in &quot; +
                       dr[&quot;loc&quot;].ToString());
}
</pre>
<p><a class="bodylink" href="http://www.oracle.com/technology/pub/articles/cook-vs08.html#lab3">Lab 3</a> (Retrieve Multiple Columns and Rows with an OracleDataReader) highlights some of these concepts.</p>
<h2>Building and Running on x64</h2>
<p>When running Visual Studio 2008 on an x64 operating system, the <strong>Active solution platform</strong> defaults to <strong>Any CPU</strong>. Change that to x86 before building your project.</p>
<div align="center">
<p><img alt="Figure 8" src="http://www.oracle.com/technology/pub/images/cook-vs08-f8.gif" /><br />
		<strong><em>Figure 8</em></strong><em> Change from <strong>Any CPU</strong> to x86 when building on a 64-bit platform</em></p>
</div>
<h2>Conclusion</h2>
<p>This article has introduced you to the process of accessing Oracle databases using .NET programming languages. You should now have the capability to connect to the database and retrieve multiple columns and rows.</p>
<p>&nbsp;</p>
<table bgcolor="#cccccc" border="0" cellpadding="4" class="bodycopy" width="100%">
<tbody>
<tr>
<td><a name="lab1"></a></p>
<h3>Lab 1: Retrieving Data from the Database</h3>
<p>We begin with the requirement that you&#39;ve created a project and added a reference as shown previously in this article.</p>
<ol>
<li>Continue by adding a button control and a label control to the Windows form. Be sure to leave room above the controls to allow additions that will be made in Lab 2.
<div align="center">
<p><img alt="Figure 9" src="http://www.oracle.com/technology/pub/images/cook-vs08-f9.gif" /><br />
								<strong><em>Figure 9</em></strong><em> Lab 1 form with button and label controls</em></p>
</p></div>
</li>
<li>Add code to retrieve data from the Oracle database and display the results on the form. Put the code in a click event handler for the button. The easiest way to get started with this task is to double click the button because it will create a stub for the event handler.
<div align="center">
<p><img alt="Figure 10" src="http://www.oracle.com/technology/pub/images/cook-vs08-f10.gif" /><br />
								<strong><em>Figure 10</em></strong><em> Click event handler stub</em></p>
</p></div>
</li>
<li>Add Visual Basic Imports statements before the Public Class declaration or C# using statements before the namespace declaration.
<pre>Imports Oracle.DataAccess.Client &#39; Visual Basic, ODP.NET Oracle managed provider

using Oracle.DataAccess.Client; // C#, ODP.NET Oracle managed provider
</pre>
</li>
<li>Add the Visual Basic version of the click event handler code between the Private Sub and End Sub statements (be sure to replace ORASRVR with your server&#39;s host name):
<pre>Dim oradb As String = &quot;Data Source=(DESCRIPTION=(ADDRESS_LIST=&quot; _
                    + &quot;(ADDRESS=(PROTOCOL=TCP)(HOST=ORASRVR)(PORT=1521)))&quot; _
                    + &quot;(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));&quot; _
                    + &quot;User Id=scott;Password=tiger;&quot;

Dim conn As New OracleConnection(oradb) &#39; Visual Basic
conn.Open()

Dim cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandText = &quot;select dname from dept where deptno = 10&quot;
cmd.CommandType = CommandType.Text

Dim dr As OracleDataReader = cmd.ExecuteReader()
dr.Read()  &#39; replace this statement in next lab
Label1.Text = dr.Item(&quot;dname&quot;) &#39; or dr.Item(0), remove in next lab

dr.Dispose()
cmd.Dispose()
conn.Dispose()
</pre>
<p>Add the following C# code to the click event handler between the { and } curly braces for the button&#39;s click event handler (be sure to replace ORASRVR with your server&#39;s host name):
<pre>string oradb = &quot;Data Source=(DESCRIPTION=(ADDRESS_LIST=&quot;
		 + &quot;(ADDRESS=(PROTOCOL=TCP)(HOST=ORASRVR)(PORT=1521)))&quot;
		 + &quot;(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));&quot;
		 + &quot;User Id=scott;Password=tiger;&quot;;

OracleConnection conn = new OracleConnection(oradb); // C#
conn.Open();

OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = &quot;select dname from dept where deptno = 10&quot;;
cmd.CommandType = CommandType.Text;

OracleDataReader dr = cmd.ExecuteReader();
dr.Read();  // replace this statement in next lab
label1.Text = dr[&quot;dname&quot;].ToString();  // remove in next lab

dr.Dispose();
cmd.Dispose();
conn.Dispose();</pre>
</li>
<li>Run the application. Click the button. You should see the following:
<div align="center">
<p><img alt="Figure 11" src="http://www.oracle.com/technology/pub/images/cook-vs08-f11.gif" /><br />
								<strong><em>Figure 11</em></strong><em> Data successfully retrieved.</em></p>
</p></div>
</li>
</ol>
</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<table bgcolor="#cccccc" border="0" cellpadding="4" class="bodycopy" width="100%">
<tbody>
<tr>
<td><a name="lab2"></a></p>
<h3>Lab 2: Adding Interactivity</h3>
<p>Now that the basics of database access are implemented in the code, the next step is to add interactivity to the application. Instead of running the hard coded query, a textbox control can be added to accept a user input for the department number (i.e., DEPTNO).</p>
<ol>
<li>Add a textbox control and another label control to the form as shown below. Set the text property of the <tt>Label2</tt> control to <tt>Enter Deptno:</tt> and make sure that the <tt>Text</tt> property of <tt>TextBox1</tt> isn&#39;t set to anything.
<div align="center">
<p><img alt="Figure 12" src="http://www.oracle.com/technology/pub/images/cook-vs08-f12.gif" /><br />
								<strong><em>Figure 12</em></strong><em> Lab 2 form with button and label controls</em></p>
</p></div>
</li>
<li>Modify the code that defines the select string:
<pre>cmd.CommandText = &quot;select dname from dept where deptno = &quot; + TextBox1.Text &#39;VB

cmd.CommandText = &quot;select dname from dept where deptno = &quot; + textBox1.Text; // C#&gt;</pre>
</li>
<li>Run the application. Test the application by entering 10 for the DEPTNO. Retest the application by entering an invalid DEPTNO (e.g., 50). The application will abort.
<div align="center">
<p><img alt="Figure 13" src="http://www.oracle.com/technology/pub/images/cook-vs08-f13.gif" /><br />
								<strong><em>Figure 13</em></strong><em> An unhandled exception</em></p>
</p></div>
</li>
<li>Modify your code to prevent an error when an invalid DEPTNO is entered. Recall that the ExecuteReader method actually returns an object. Replace the line containing dr.Read with all of the following statements.
<pre>If dr.Read() Then &#39; Visual Basic
    Label1.Text = dr.Item(&quot;dname&quot;).ToString()
Else
    Label1.Text = &quot;deptno not found&quot;
End If

if (dr.Read()) // C#
{
    label1.Text = dr[&quot;dname&quot;].ToString();;
}
else
{
    label1.Text = &quot;deptno not found&quot;;
}
</pre>
<p>Test the application by entering a DEPTNO that does not exist. Now the application no longer aborts. Enter the letter A instead of a number and click the button. The application aborts. Clearly, our application needs a better approach to handling errors.</p>
<p>Although it could be argued that the application should not allow the user to make invalid inputs that would cause an error, ultimately the application must have robust error handling added. Not all errors are preventable, so error handling must be implemented.</p>
</li>
</ol>
</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<table bgcolor="#cccccc" border="0" cellpadding="4" class="bodycopy" width="100%">
<tbody>
<tr>
<td><a name="lab3"></a></p>
<h3>Lab 3: Retrieve Multiple Columns and Rows with an OracleDataReader</h3>
<p>Now that a single value has been retrieved, the next step is to retrieve multiple columns and rows with an OracleDataReader. A ListBox control is added to the form to display the results.</p>
<ol>
<li>Add a ListBox control to the form. Resize the control to fill most of the width of the form as shown below.
<div align="center">
<p><img alt="Figure 14" src="http://www.oracle.com/technology/pub/images/cook-vs08-f14.gif" /><br />
								<strong><em>Figure 14</em></strong><em> Form with ListBox added</em></p>
</p></div>
</li>
<li>Remove the where clause from the query and add the additional columns:
<pre>cmd.CommandText = &quot;select deptno, dname, loc from dept&quot; &#39; Visual Basic

cmd.CommandText = &quot;select deptno, dname, loc from dept&quot;; // C#</pre>
</li>
<li>The query results will be read in a while loop and will populate the ListBox control. Modify your Visual Basic code to look like this:
<pre>Dim oradb As String = &quot;Data Source=(DESCRIPTION=(ADDRESS_LIST=&quot; _
            + &quot;(ADDRESS=(PROTOCOL=TCP)(HOST=ORASRVR)(PORT=1521)))&quot; _
            + &quot;(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));&quot; _
            + &quot;User Id=scott;Password=tiger;&quot;

Dim conn As New OracleConnection(oradb) &#39; Visual Basic
conn.Open()

Dim cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandText = &quot;select deptno, dname, loc from dept&quot;
cmd.CommandType = CommandType.Text

Dim dr As OracleDataReader = cmd.ExecuteReader()
While dr.Read()
    ListBox1.Items.Add(&quot;The &quot; + dr.Item(&quot;dname&quot;) + _
                       &quot; department is in &quot; + dr.Item(&quot;loc&quot;))
End While

dr.Dispose()
cmd.Dispose()
conn.Dispose()
Modify your C# code to look like this:
string oradb = &quot;Data Source=(DESCRIPTION=(ADDRESS_LIST=&quot;
                + &quot;(ADDRESS=(PROTOCOL=TCP)(HOST=ORASRVR)(PORT=1521)))&quot;
                + &quot;(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));&quot;
                + &quot;User Id=scott;Password=tiger;&quot;;

OracleConnection conn = new OracleConnection(oradb); // C#
conn.Open();

OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = &quot;select deptno, dname, loc from dept&quot;;
cmd.CommandType = CommandType.Text;

OracleDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
   listBox1.Items.Add(&quot;The &quot; + dr[&quot;dname&quot;].ToString() +
                      &quot; department is in &quot; + dr[&quot;loc&quot;].ToString());
}

dr.Dispose();
cmd.Dispose();
conn.Dispose();</pre>
</li>
<li>Run the application. The ListBox should be populated with all of the department names and locations from the DEPT table. The code downloads have error handling implemented.</li>
</ol>
</td>
</tr>
</tbody>
</table>
<hr />
<p><strong>John Paul Cook</strong> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.allfreetech.com/microsoft-net/build-a-net-application-on-the-oracle-database-with-visual-studio-2005-or-2008-115.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use Microsoft Visual Studio for the complete .NET and Oracle Database development lifecycle</title>
		<link>http://www.allfreetech.com/microsoft-net/use-microsoft-visual-studio-for-the-complete-net-and-oracle-database-development-lifecycle-117.html</link>
		<comments>http://www.allfreetech.com/microsoft-net/use-microsoft-visual-studio-for-the-complete-net-and-oracle-database-development-lifecycle-117.html#comments</comments>
		<pubDate>Mon, 04 Jan 2010 07:35:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Microsoft .Net]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://www.allfreetech.com/?p=117</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p><span class="bodycopy">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. <span id="more-117"></span></span></p>
<p><span class="bodycopy">In this article, I&rsquo;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</span></p>
<p>&nbsp;</p>
<ul>
<li><span class="bodycopy">Create users and roles and grant privileges to them</span></li>
<li><span class="bodycopy">Import tables and table data from Microsoft Excel spreadsheets (and many third-party databases) </span></li>
<li><span class="bodycopy">Automatically generate .NET code for an ASP.NET Web application</span></li>
<li><span class="bodycopy">Use Microsoft Query Builder to design SQL</span></li>
<li><span class="bodycopy">Generate a SQL script for all the Oracle schema objects your application uses</span></li>
<li><span class="bodycopy">Check your application and related SQL scripts into source control from Visual Studio </span></li>
</ul>
<p><span class="bodycopy">I&rsquo;ll also touch on other aspects of the development lifecycle, including PL/SQL debugging and performance tuning. </span></p>
<p><strong><span class="parahead1">Setup</span> </strong></p>
<p><span class="bodycopy">To follow along with the steps in this article, you will need the following:</span></p>
<p>&nbsp;</p>
<ul>
<li><span class="bodycopy">Oracle9<i>i</i> Database Release 9.2 or later (SYSDBA privileges are required for a few steps) </span></li>
<li><span class="bodycopy">Oracle Developer Tools for Visual Studio Release 11.1.0.7.10 (beta) or later </span></li>
<li><span class="bodycopy">Microsoft Visual Studio 2008 or Visual Studio 2005</span></li>
<li><span class="bodycopy">A source control system with a plug-in for Visual Studio, such as Microsoft SourceSafe, Team Foundation source control, or Subversion</span></li>
</ul>
<p><span class="bodycopy">To connect to Oracle Database from Visual Studio, create a connection in Server Explorer. (If Server Explorer is not yet visible, select </span><span class="boldbodycopy">View</span><span class="bodycopy"> -&gt; </span><span class="boldbodycopy">Server Explorer</span><span class="bodycopy"> from the main menu.) Now create the new connection in </span><span class="boldbodycopy">Data Connections</span><span class="bodycopy">, the top-level node of Server Explorer. Right-click </span><span class="boldbodycopy">Data Connections</span><span class="bodycopy">, and select </span><span class="boldbodycopy">Add Connection</span><span class="bodycopy">. In the dialog box that appears, confirm that </span><span class="boldbodycopy">Oracle Database (Oracle ODP.NET)</span><span class="bodycopy"> appears in the </span><span class="boldbodycopy">Data source</span><span class="bodycopy"> field. If you do not see </span><span class="boldbodycopy">Oracle Database (Oracle ODP.NET)</span><span class="bodycopy">, click the </span><span class="boldbodycopy">Change</span><span class="bodycopy"> button and select </span><span class="boldbodycopy">Oracle Data Provider for .NET</span><span class="bodycopy"> from the list of providers. Select the database you want to connect to from the </span><span class="boldbodycopy">Datasource name</span><span class="bodycopy"> list. Click </span><span class="boldbodycopy">Use a specific user name and password</span><span class="bodycopy">. In the </span><span class="boldbodycopy">User name</span><span class="bodycopy"> field, enter </span><tt>SYS</tt><span class="bodycopy">. In the </span><span class="boldbodycopy">Password</span><span class="bodycopy"> field, enter the password for SYS, and ensure that </span><span class="boldbodycopy">Role</span><span class="bodycopy"> is set to </span><span class="boldbodycopy">SYSDBA</span><span class="bodycopy">. Click </span><span class="boldbodycopy">Test connection</span><span class="bodycopy"> to verify that you can connect, and click </span><span class="boldbodycopy">OK</span><span class="bodycopy"> to complete the connection setup. </span></p>
<p><strong><span class="parahead1">Creating a New User with User Designer</span> </strong></p>
<p><span class="bodycopy">Now create an Oracle Database user that you will use during development. In Server Explorer, expand the new </span><span class="boldbodycopy">SYS</span><span class="bodycopy"> connection you just created. You should see two child nodes: </span><span class="boldbodycopy">Schemas</span><span class="bodycopy"> and </span><span class="boldbodycopy">Roles</span><span class="bodycopy">. Right-click </span><span class="boldbodycopy">Schemas</span><span class="bodycopy">, and choose </span><span class="boldbodycopy">New User</span><span class="bodycopy"> from the context menu. This will launch User Designer. </span></p>
<p><span class="bodycopy">I&rsquo;m creating a user named CSHAY, so in User Designer, for </span><span class="boldbodycopy">User name</span><span class="bodycopy">, I enter </span><tt>CSHAY</tt><span class="bodycopy"> (see Figure 1). For </span><span class="boldbodycopy">Profile</span><span class="bodycopy"> I leave the </span><span class="boldbodycopy">&ldquo;DEFAULT&rdquo;</span><span class="bodycopy"> value, and for </span><span class="boldbodycopy">Authentication</span><span class="bodycopy"> I select </span><span class="boldbodycopy">Password</span><span class="bodycopy">. For </span><span class="boldbodycopy">Password</span><span class="bodycopy"> and </span><span class="boldbodycopy">Confirm password</span><span class="bodycopy">, 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 </span><span class="boldbodycopy">USERS</span><span class="bodycopy"> tablespace, and for the temporary tablespace, I choose </span><span class="boldbodycopy">TEMP</span><span class="bodycopy">. Finally, I click </span><span class="boldbodycopy">Save</span><span class="bodycopy">, which will create my new user account. The SQL that creates the new user account will appear in the output window in Visual Studio.</span></p>
<p>&nbsp;</p>
<table align="center" width="100%">
<tbody>
<tr>
<td align="center"><img alt="figure 1" border="0" height="458" src="http://www.oracle.com/technology/oramag/oracle/09-jul/images/o49odt_f1.gif" width="458" /></td>
</tr>
<tr>
<td align="center"><span class="bodycopy">Figure 1: Creating a user in User Designer</span></td>
</tr>
</tbody>
</table>
<p><strong><span class="parahead1">Granting Privileges to the New User Account</span> </strong></p>
<p><span class="bodycopy">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 </span><span class="boldbodycopy">SYS</span><span class="bodycopy"> connection node and choose </span><span class="boldbodycopy">Privileges</span><span class="bodycopy"> from the context menu. In the Grant/Revoke Privileges dialog box that appears, set </span><span class="boldbodycopy">Object Type</span><span class="bodycopy"> to </span><span class="boldbodycopy">Role</span><span class="bodycopy">. Select </span><span class="boldbodycopy">User</span><span class="bodycopy">, and then select </span><span class="boldbodycopy">CSHAY</span><span class="bodycopy"> from the list of users. At the bottom of the dialog box, there will be a list of roles. Select both the </span><span class="boldbodycopy">CONNECT</span><span class="bodycopy"> and the </span><span class="boldbodycopy">RESOURCE</span><span class="bodycopy"> roles, and then click </span><span class="boldbodycopy">Apply</span><span class="bodycopy">.</span></p>
<p><span class="bodycopy">In Server Explorer, again right-click the </span><span class="boldbodycopy">SYS</span><span class="bodycopy"> connection node and choose </span><span class="boldbodycopy">Privileges</span><span class="bodycopy"> to reopen the Grant/Revoke Privileges dialog box. This time set </span><span class="boldbodycopy">Object Type</span><span class="bodycopy"> to </span><span class="boldbodycopy">System Privileges</span><span class="bodycopy">. Select </span><span class="boldbodycopy">User</span><span class="bodycopy">, and then select </span><span class="boldbodycopy">CSHAY</span><span class="bodycopy"> from the list of users. At the bottom of the dialog box will be a list of privileges. Select the </span><span class="boldbodycopy">CREATE ROLE</span><span class="bodycopy"> privilege, and click </span><span class="boldbodycopy">Apply</span><span class="bodycopy">. Finally, click </span><span class="boldbodycopy">OK</span><span class="bodycopy"> to dismiss the dialog box. </span></p>
<p><span class="bodycopy">Now connect to Oracle Database, using the new CSHAY user in Server Explorer. Right-click </span><span class="boldbodycopy">Data Connections</span><span class="bodycopy">, and select </span><span class="boldbodycopy">Add Connection</span><span class="bodycopy">. Follow the same steps to connect as CSHAY that you used to connect as </span><span class="boldbodycopy">SYS</span><span class="bodycopy">, except set </span><span class="boldbodycopy">Role</span><span class="bodycopy"> to </span><span class="boldbodycopy">Default</span><span class="bodycopy">.</span></p>
<p><strong><span class="parahead1">Importing Tables and Table Data</span> </strong></p>
<p><span class="bodycopy">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 </span><span class="boldbodycopy">Tools</span><span class="bodycopy"> -&gt; </span><span class="boldbodycopy">Run SQL Plus Script</span><span class="bodycopy"> 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.</span></p>
<p><span class="bodycopy">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 <a href="http://www.oracle.com/technology/oramag/oracle/09-jul/o49odt.html#next"><span class="bodylink">Next Steps box</span></a> at the end of this article.</span></p>
<p><span class="bodycopy">First, connect to the EMP_DEPT.xls spreadsheet in Server Explorer. Right-click </span><span class="boldbodycopy">Data Connections</span><span class="bodycopy"> and select </span><span class="boldbodycopy">Add Connection</span><span class="bodycopy">. In the dialog box that appears, click the </span><span class="boldbodycopy">Change</span><span class="bodycopy"> button and select </span><span class="boldbodycopy">&lt;other&gt;</span><span class="bodycopy"> from the list. In the </span><span class="boldbodycopy">Data Provider</span><span class="bodycopy"> list, select </span><span class="boldbodycopy">.NET Framework Data Provider for OLEDB</span><span class="bodycopy"> and then click </span><span class="boldbodycopy">OK</span><span class="bodycopy">. In Server Explorer, right-click </span><span class="boldbodycopy">Data Connections</span><span class="bodycopy"> and select </span><span class="boldbodycopy">Add Connection</span><span class="bodycopy">, and in the dialog box, under OLEDB provider, select </span><span class="boldbodycopy">Microsoft Jet 4.0 OLEDB Provider</span><span class="bodycopy">. (Note that if you wanted to use the new Excel 12.0 .xlsx spreadsheet format, you would need to select </span><span class="boldbodycopy">Microsoft Office 12.0 Access Database Engine OLEDB Provider</span><span class="bodycopy">.) In the </span><span class="boldbodycopy">Server or File name</span><span class="bodycopy"> field, enter the full path to EMP_DEPT.xls, and click the </span><span class="boldbodycopy">Advanced</span><span class="bodycopy"> button, which will open a property sheet. For the </span><span class="boldbodycopy">Extended Properties</span><span class="bodycopy"> property, enter </span><tt>Excel 8.0;HDR=YES</tt><span class="bodycopy">. (If you were using the Excel 12.0 .xlsx format, you would enter </span><tt>Excel 12.0;HDR=YES</tt><span class="bodycopy">.) Click </span><span class="boldbodycopy">OK</span><span class="bodycopy"> to close the property sheet, click </span><span class="boldbodycopy">Test</span><span class="bodycopy"> to test the connection, and click </span><span class="boldbodycopy">OK</span><span class="bodycopy"> once more to close the Add Connection dialog box. </span></p>
<p><span class="bodycopy">Now import the data from the connected spreadsheet. Right-click the </span><span class="boldbodycopy">CSHAY</span><span class="bodycopy"> connection node, and select </span><span class="boldbodycopy">Import Table</span><span class="bodycopy">. This will start the Import Table Wizard. Click </span><span class="boldbodycopy">Next</span><span class="bodycopy"> until you see the Choose Data Source Connection screen. For the data connection source, choose the </span><span class="boldbodycopy">emp_dept.xls</span><span class="bodycopy"> connection. Click </span><span class="boldbodycopy">Next</span><span class="bodycopy"> to go to the Select Tables to Import screen. Under Source Table, click the check boxes next to both </span><span class="boldbodycopy">DEPARTMENTS$</span><span class="bodycopy"> and </span><span class="boldbodycopy">EMPLOYEES$</span><span class="bodycopy">. Click each destination table name, and then edit each name to remove the dollar sign at the end (see Figure 2). The </span><span class="boldbodycopy">Import Data</span><span class="bodycopy"> check box is checked by default, which is what you want. </span></p>
<p>&nbsp;</p>
<table align="center" width="100%">
<tbody>
<tr>
<td align="center"><img alt="figure 2" border="0" height="406" src="http://www.oracle.com/technology/oramag/oracle/09-jul/images/o49odt_f2.gif" width="650" /></td>
</tr>
<tr>
<td align="center"><span class="bodycopy">Figure 2: Import Table Wizard: Renaming the destination table</span></td>
</tr>
</tbody>
</table>
<p><span class="bodycopy">Click </span><span class="boldbodycopy">Next</span><span class="bodycopy"> to go to the Customize Columns Settings screen (see Figure 3). For each column of type BINARY_DOUBLE, highlight the row, and in the </span><span class="boldbodycopy">Data Type</span><span class="bodycopy"> list, choose </span><span class="boldbodycopy">NUMBER</span><span class="bodycopy">. For EMPLOYEES.EMPLOYEE_ID, uncheck the </span><span class="boldbodycopy">Allows NULL</span><span class="bodycopy"> check box and then check the </span><span class="boldbodycopy">Primary Key</span><span class="bodycopy"> check box. Do the same for DEPARTMENTS .DEPARTMENT_ID. (Note that you will need to use the </span><span class="boldbodycopy">Select a Table</span><span class="bodycopy"> list at the top of the screen to switch between the two tables.) Click </span><span class="boldbodycopy">Finish</span><span class="bodycopy"> to import the data.</span></p>
<p>&nbsp;</p>
<table align="center" width="100%">
<tbody>
<tr>
<td align="center"><img alt="figure 3" border="0" height="411" src="http://www.oracle.com/technology/oramag/oracle/09-jul/images/o49odt_f3.gif" width="650" /></td>
</tr>
<tr>
<td align="center"><span class="bodycopy">Figure 3: Import Table Wizard: Choosing the destination column datatype</span></td>
</tr>
</tbody>
</table>
<p><span class="bodycopy">After the data has been successfully imported, click </span><span class="boldbodycopy">OK</span><span class="bodycopy"> and then go to Server Explorer, right-click the </span><span class="boldbodycopy">CSHAY</span><span class="bodycopy"> connection, and select </span><span class="boldbodycopy">Refresh</span><span class="bodycopy">. Navigate to the </span><span class="boldbodycopy">Tables</span><span class="bodycopy"> node to view the newly imported tables. </span></p>
<p><strong><span class="parahead1">Automatically Generating the .NET Code for a Web Application</span> </strong></p>
<p><span class="bodycopy">Now, taking advantage of Visual Studio&rsquo;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. </span></p>
<p><span class="bodycopy">From the Visual Studio main menu, select </span><span class="boldbodycopy">File</span><span class="bodycopy"> -&gt; </span><span class="boldbodycopy">New Project</span><span class="bodycopy">. For </span><span class="boldbodycopy">Visual C# Project Type</span><span class="bodycopy"> select </span><span class="boldbodycopy">Web</span><span class="bodycopy">, and then select the </span><span class="boldbodycopy">ASP.NET Web Application</span><span class="bodycopy"> template. Click the </span><span class="boldbodycopy">Design</span><span class="bodycopy"> button, below the HTML code window. If the toolbox is not already visible, choose </span><span class="boldbodycopy">View</span><span class="bodycopy"> -&gt; </span><span class="boldbodycopy">Toolbox</span><span class="bodycopy"> from the main menu.</span></p>
<p><span class="bodycopy">Scroll down to the Data section of the toolbox, and drag and drop a </span><span class="boldbodycopy">GridView</span><span class="bodycopy"> control onto the design surface. The GridView Tasks window should immediately open, but if it does not, click the small </span><span class="boldbodycopy">&gt;</span><span class="bodycopy"> icon to open it. From the </span><span class="boldbodycopy">Choose Data Source</span><span class="bodycopy"> list, select </span><span class="boldbodycopy">:&lt;New Data Source&gt;</span><span class="bodycopy">. This will open the Data Source Configuration Wizard to the Choose a Data Source Type screen. Select the </span><span class="boldbodycopy">Database</span><span class="bodycopy"> icon, and then click </span><span class="boldbodycopy">OK</span><span class="bodycopy">. On the Choose Your Data Connection screen (the next screen), select </span><span class="boldbodycopy">CSHAY</span><span class="bodycopy"> for the data connection, and click </span><span class="boldbodycopy">Next</span><span class="bodycopy">. On the Save a Connection String to the Application Configuration File screen, accept the defaults and click </span><span class="boldbodycopy">Next</span><span class="bodycopy">.</span></p>
<p><span class="bodycopy">On the Configure the Select Statement screen, select </span><span class="boldbodycopy">Specify a Custom SQL Statement</span><span class="bodycopy"> and click </span><span class="boldbodycopy">Next</span><span class="bodycopy">. On the Define Custom Statements screen, click the </span><span class="boldbodycopy">Query Builder</span><span class="bodycopy"> button. When Microsoft Query Builder launches and the Add Table dialog box appears, select the </span><span class="boldbodycopy">DEPARTMENTS</span><span class="bodycopy"> and </span><span class="boldbodycopy">EMPLOYEES</span><span class="bodycopy"> tables and click the </span><span class="boldbodycopy">Add</span><span class="bodycopy"> button. In the top section of Query Builder, you will see both tables automatically joined by </span><span class="boldbodycopy">DEPARTMENT_ID</span><span class="bodycopy"> (see Figure 4).</span></p>
<p>&nbsp;</p>
<table align="center" width="100%">
<tbody>
<tr>
<td align="center"><img alt="figure 4" border="0" height="508" src="http://www.oracle.com/technology/oramag/oracle/09-jul/images/o49odt_f4.gif" width="650" /></td>
</tr>
<tr>
<td align="center"><span class="bodycopy">Figure 4: Microsoft Query Builder </span></td>
</tr>
</tbody>
</table>
<p><span class="bodycopy">Now you need to decide which columns to select in your query. In the </span><span class="boldbodycopy">EMPLOYEES</span><span class="bodycopy"> table, select the </span><span class="boldbodycopy">EMPLOYEE_ID</span><span class="bodycopy">, </span><span class="boldbodycopy">FIRST_NAME, LAST_NAME</span><span class="bodycopy">, and </span><span class="boldbodycopy">SALARY</span><span class="bodycopy"> columns. In the </span><span class="boldbodycopy">DEPARTMENTS</span><span class="bodycopy"> table, select the </span><span class="boldbodycopy">DEPARTMENT_NAME</span><span class="bodycopy"> column. In the second pane from the top, order the column list by dragging and dropping so that </span><span class="boldbodycopy">EMPLOYEE_ID</span><span class="bodycopy"> is the first </span><span class="boldbodycopy">Select</span><span class="bodycopy"> list item and </span><span class="boldbodycopy">DEPARTMENT_NAME</span><span class="bodycopy"> is last. Click the </span><span class="boldbodycopy">Execute Query</span><span class="bodycopy"> button to test the query. Click </span><span class="boldbodycopy">OK</span><span class="bodycopy"> to finish and return to the Define Custom Statements screen of the Data Source Configuration Wizard. Click </span><span class="boldbodycopy">Next</span><span class="bodycopy">; test the query again (if you wish), by clicking the </span><span class="boldbodycopy">Test Query</span><span class="bodycopy"> button; and then click </span><span class="boldbodycopy">Finish</span><span class="bodycopy">.</span></p>
<p><span class="bodycopy">From the main menu, select </span><span class="boldbodycopy">Debug</span><span class="bodycopy"> -&gt; </span><span class="boldbodycopy">Start Without Debugging</span><span class="bodycopy">, which will open your Web browser and display your running application (see Figure 5).</span></p>
<p>&nbsp;</p>
<table align="center" width="100%">
<tbody>
<tr>
<td align="center"><img alt="figure 5" border="0" height="451" src="http://www.oracle.com/technology/oramag/oracle/09-jul/images/o49odt_f5.gif" width="610" /></td>
</tr>
<tr>
<td align="center"><span class="bodycopy">Figure 5: The application running in a Web browser</span></td>
</tr>
</tbody>
</table>
<p><strong><span class="parahead1">Creating an Oracle Database Project</span> </strong></p>
<p><span class="bodycopy">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.</span></p>
<p><span class="bodycopy">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 </span><span class="boldbodycopy">Add</span><span class="bodycopy"> -&gt; </span><span class="boldbodycopy">New Project</span><span class="bodycopy"> from the context menu. Under Project Types, select </span><span class="boldbodycopy">Other Project Types</span><span class="bodycopy"> and then </span><span class="boldbodycopy">Database</span><span class="bodycopy">, and then select the </span><span class="boldbodycopy">Oracle Database Project</span><span class="bodycopy"> template. When asked to select a database reference to be stored with the project, choose the </span><span class="boldbodycopy">CSHAY</span><span class="bodycopy"> connection and click </span><span class="boldbodycopy">OK</span><span class="bodycopy">.</span></p>
<p><strong><span class="parahead1">Creating a Role for the Application</span> </strong></p>
<p><span class="bodycopy">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&rsquo;s sample application, for example, will need access to the </span><span class="boldbodycopy">EMPLOYEES</span><span class="bodycopy"> and </span><span class="boldbodycopy">DEPARTMENTS</span><span class="bodycopy"> tables.</span></p>
<p><span class="bodycopy">To create the application user role, right-click the </span><span class="boldbodycopy">Roles</span><span class="bodycopy"> node under the </span><span class="boldbodycopy">CSHAY</span><span class="bodycopy"> connection, and select </span><span class="boldbodycopy">New Role</span><span class="bodycopy"> from the context menu. Enter </span><tt>EMPDEPT_WEBAPP</tt><span class="bodycopy"> in the </span><span class="boldbodycopy">Role Name</span><span class="bodycopy"> field, and then click </span><span class="boldbodycopy">Save</span><span class="bodycopy">.</span></p>
<p><span class="bodycopy">Next, grant privileges to the role you just created. Right-click the </span><span class="boldbodycopy">EMPDEPT_WEBAPP</span><span class="bodycopy"> node under the </span><span class="boldbodycopy">Roles</span><span class="bodycopy"> node, and select </span><span class="boldbodycopy">Privileges</span><span class="bodycopy"> from the context menu. When the Privilege Wizard launches, set </span><span class="boldbodycopy">Object Type</span><span class="bodycopy"> to </span><span class="boldbodycopy">Table</span><span class="bodycopy"> (see Figure 6). The </span><span class="boldbodycopy">Schema</span><span class="bodycopy"> field should be set to </span><span class="boldbodycopy">CSHAY</span><span class="bodycopy">. Select </span><span class="boldbodycopy">Grant/Revoke privileges to a user/role on one or more database object(s)</span><span class="bodycopy">, and then in the </span><span class="boldbodycopy">Object Name(s)</span><span class="bodycopy"> list, select both </span><span class="boldbodycopy">DEPARTMENTS</span><span class="bodycopy"> and </span><span class="boldbodycopy">EMPLOYEES</span><span class="bodycopy"> (by holding down the Shift key while you click). Select </span><span class="boldbodycopy">Role</span><span class="bodycopy">, and then select </span><span class="boldbodycopy">EMPDEPT_WEBAPP</span><span class="bodycopy"> from the list of roles. </span></p>
<p>&nbsp;</p>
<table align="center" width="100%">
<tbody>
<tr>
<td align="center"><img alt="figure 6" border="0" height="592" src="http://www.oracle.com/technology/oramag/oracle/09-jul/images/o49odt_f6.gif" width="547" /></td>
</tr>
<tr>
<td align="center"><span class="bodycopy">Figure 6: Granting permissions on tables to the EMPDEPT_WEBAPP role</span></td>
</tr>
</tbody>
</table>
<p><span class="bodycopy">At the bottom of the dialog box, there will be a list of privileges. Select the </span><span class="boldbodycopy">DELETE</span><span class="bodycopy">, </span><span class="boldbodycopy">INSERT</span><span class="bodycopy">, </span><span class="boldbodycopy">SELECT</span><span class="bodycopy">, and </span><span class="boldbodycopy">UPDATE</span><span class="bodycopy"> privileges. Click the </span><span class="boldbodycopy">Preview SQL</span><span class="bodycopy"> button, and then click </span><span class="boldbodycopy">Add to Project</span><span class="bodycopy">. 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 </span><span class="boldbodycopy">OK</span><span class="bodycopy"> to dismiss the Preview SQL window, and then click the </span><span class="boldbodycopy">Apply</span><span class="bodycopy"> button to execute the SQL needed to grant the privileges. Then click </span><span class="boldbodycopy">OK</span><span class="bodycopy"> to dismiss the dialog box. </span></p>
<p><strong><span class="parahead1">Generating SQL Scripts</span> </strong></p>
<p><span class="bodycopy">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 </span><span class="boldbodycopy">CSHAY</span><span class="bodycopy"> node, while holding down the Control key, select the </span><span class="boldbodycopy">DEPARTMENTS</span><span class="bodycopy"> table, the </span><span class="boldbodycopy">EMPLOYEES</span><span class="bodycopy"> table, and the </span><span class="boldbodycopy">EMPDEPT_WEBAPP</span><span class="bodycopy"> role. With all three selected, right-click and select </span><span class="boldbodycopy">Generate Create Script to Project.</span><span class="bodycopy"> Provide a script name, and click </span><span class="boldbodycopy">OK</span><span class="bodycopy">. After the script is added, the Oracle SQL script editor will open and display the script (see Figure 7). </span></p>
<p>&nbsp;</p>
<table align="center" width="100%">
<tbody>
<tr>
<td align="center"><img alt="figure 7" border="0" height="363" src="http://www.oracle.com/technology/oramag/oracle/09-jul/images/o49odt_f7.gif" width="650" /></td>
</tr>
<tr>
<td align="center"><span class="bodycopy">Figure 7: The Oracle SQL script editor (left) and project with source control menu items (right)</span></td>
</tr>
</tbody>
</table>
<p><span class="bodycopy">Under Solution Explorer, in the </span><span class="boldbodycopy">Security</span><span class="bodycopy"> -&gt; </span><span class="boldbodycopy">Permissions</span><span class="bodycopy"> 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. </span></p>
<p><strong><span class="parahead1">Adding Application and SQL Scripts into Source Control</span> </strong></p>
<p><span class="bodycopy">The first time you use source control in Visual Studio, you will need to enable it. From the main menu, select </span><span class="boldbodycopy">Tools</span><span class="bodycopy"> -&gt; </span><span class="boldbodycopy">Options</span><span class="bodycopy">, and then select </span><span class="boldbodycopy">Source Control</span><span class="bodycopy">. Under </span><span class="boldbodycopy">Current Source Control Plug-in</span><span class="bodycopy">, select the plug-in corresponding to your source control system. If you don&rsquo;t see it listed here, you will need to obtain it from the vendor and install it.</span></p>
<p><span class="bodycopy">To add your solution into source control, right-click the solution name in Solution Explorer and select </span><span class="boldbodycopy">Add Solution to Source Control</span><span class="bodycopy">. After the solution is added to source control, new icons will appear next to the filenames and source control menu items such as </span><span class="boldbodycopy">Check Out for Edit</span><span class="bodycopy"> 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 </span><span class="boldbodycopy">Run</span><span class="bodycopy"> from the context menu. This will ensure that their database schema matches the one required by the application. </span></p>
<p><strong><span class="parahead1">Debugging Your Application&rsquo;s PL/SQL </span> </strong></p>
<p><span class="bodycopy">Although this application doesn&rsquo;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 </span><span class="italicbodycopy">Oracle Magazine</span><span class="bodycopy"> article (&ldquo;<a href="http://www.oracle.com/technology/oramag/oracle/06-sep/o56odp.html" target="_blank"><span class="bodylink">Debugging PL/SQL from .NET</span></a>&rdquo;). </span></p>
<p><strong><span class="parahead1">Performance Tuning</span> </strong></p>
<p><span class="bodycopy">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 </span><span class="boldbodycopy">Oracle Performance Analyzer</span><span class="bodycopy"> 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&rsquo;ll be deep-diving into Oracle Performance Analyzer and performance tuning tricks in an upcoming article.</span></p>
<p><strong><span class="boldbodycopy">Christian Shay</span><span class="bodycopy"> </span></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.allfreetech.com/microsoft-net/use-microsoft-visual-studio-for-the-complete-net-and-oracle-database-development-lifecycle-117.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

