The release number of this article has been CHS308448
For Microsoft Visual Basic .NET versions of this article, see
308071.
This article references the following Microsoft .NET Framework Class Bank Name Space:
System.Data.Oledb
This task content
summary
Steps to access the Oracle database
SUMMARY This article demonstrates how to use the ADO.NET OLE DB hosting provider to access the Oracle database.
Back to top
Require the following list lists the recommended hardware, software, network structure, and service pack required:
Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server or Windows NT 4.0 Server Oracle Client Tools (installed on your computer) Microsoft Visual Studio .NET This article assumes that you are familiar with the following topics:
Visual Studio .NET ADO.NET Foundation and Syntax Oracle Connection
Back to top
Steps to access the Oracle database
In Oracle, create a table named TestTable, as shown below: Create Table TestTable (C1 Char (5)); insert the data into TestTable, as shown below: Insert Into TestTable C1 Values ('Test1');
INSERT INTO TESTTABLE C1 VALUES ('Test2');
INSERT INTO TESTTTABLE C1 VALUES ('Test3'); Start Visual Studio .NET. Create a Windows application project in Visual C # .NET. Make sure your project contains a reference to System.Data namespace, if not included, add a reference to this namespace. Drag a Button control to FORM1 and change its Name property to btnTest. Use the USING statement for System, System.Data, and System.Data.Oledb namespace, so you don't need to limit the declarations in these namespaces in your code. Using system;
Using system.data;
Using system.data.oledb; Switch to Forms view, then double-click BTNTest to add a click event handler. Add the following code to the handler: string sconnectionString =
"Provider = msdaora.1; user id = myuid; password = mypwd;
Data Source = MyoracleServer; Persist Security Info = FALSE ";
String myselectQuery =
"SELECT * from TestTable WHERE C1 LIKE?";
OLEDBCONNECTION MyConnection = New OLEDBConnection (Sconnectionstring);
OLEDBCommand mycommand = new oledbcommand (MySelectQuery, MyConnection);
MyCommand.Parameters.add ("@ p1", oledbtype.char, 5) .value = "test%";
MyConnection.open (); OLEDBDATAREADER MyReader = MyCommand.executeReader ();
INT recordcount = 0;
Try
{
While (MyReader.Read ())
{
Recordcount = RecordCount 1;
Messagebox.show (MyReader.getstring (0) .tostring ());
}
IF (RecordCount == 0)
{
MessageBox.show ("no data returned");
}
Else
{
Messagebox.show ("Number of Records Returned:" RecordCount);
}
}
Catch (Exception EX)
{
Messagebox.show (ex.totring ());
}
Finally
{
MyReader.Close ();
MyConnection.Close ();
} Save the project. On the Debug menu, click Start to run your project. Click this button to display the data.
Back to top
Refer to additional information, click the following article number to see the article in the Microsoft Knowledge Base:
176936 INFO: Visual Basic Accessing An Oracle Database Using ADO (Info) Using ADO in Visual Basic Applications For more information on ADO.NET objects and syntax, see Microsoft .NET Framework SDK (Software Development Kit) documentation Or the following topics in MSDN Online:
Use ADO.NET access data http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaccessingDatawithadonet.asp
Back to top
The information in this article applies to:
Microsoft ADO.NET (provided with .NET Frame) Microsoft Visual C # .NET (2002)
Reserved: 2002-6-17 (1.0) Keyword Kbhowto KbhowTomaster Kboledb KbsystemData KB308448