Connecting to Different Databases Using ASP.NET 2.0 - Connecting to an Oracle database using the GenericDatabase class
(Page 5 of 5 )
The previous section concentrated on using the GenericDatabase class to connect and work with Microsoft SQL Server. In this section, I shall continue with the same class and work with Oracle databases.
Before you begin, modify your web.config as following:
<configuration>
<appSettings/>
<connectionStrings>
<!--<add name="AdventureWorks"
connectionString="Database=AdventureWorks;Server=(local)
SQL2k5;Integrated Security=SSPI;"
providerName="System.Data.SqlClient"/> -->
<add name="MyOracleDB" connectionString= "Provider=msdaora;Data
Source=MyOracleDB;User Id=UserName;Password=asdasd;" />
</connectionStrings>
The following code uses the GenericDatabase class to connect to a Microsoft SQL Server database using OLEDB connection string.
ImportsSystem.Data
ImportsMicrosoft.Practices.EnterpriseLibrary.Data
PartialClass _Default
Inherits System.Web.UI.Page
Protected Sub btnConnect_Click(ByVal sender As Object, ByVal
e As System.EventArgs) Handles btnConnect.Click
Dim db As New GenericDatabase
(ConfigurationManager.ConnectionStrings
("MyOracleDB").ConnectionString,
System.Data.OleDb.OleDbFactory.Instance)
Me.lblMsg.Text = db.ConnectionStringWithoutCredentials
Dim dt As DataTable = db.ExecuteDataSet(CommandType.Text,
"select * from Scott.emp").Tables(0)
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
End Sub
EndClass
Even though I used the GenericDatabase class to connect to Oracle, you can also connect using the OracleDatabase as follows:
Dim db As OracleDatabase = DirectCast
(DatabaseFactory.CreateDatabase("MyOracleDb"), OracleDatabase)
It all depends on your needs. In my upcoming contributions, we shall look into the most used routines in the Data Access Application Block (along with accessing stored procedures). Don’t forget to check back or sign up for a newsletter to notify you!
The entire source code for this article is available in the form of a downloadable zip. The solution was developed using Microsoft Visual Studio 2005 Professional Edition on Microsoft Windows Server 2003 Enterprise Edition together with Microsoft SQL Server 2005 Developer Edition and Microsoft Enterprise Library for .NET Framework 2.0 (January 2006 version). I didn’t really test the solution with any other/previous editions. If you have any problems in executing the solution, please post in the discussion area.
Any feedback, suggestions, bugs, errors, improvements etc., are highly appreciated at jag_chat@yahoo.com.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |