How to connect to an Oracle Database in VB

Posted on 2012-08-23
Last Modified: 2012-08-24
I've connected many times to databases using a connection String placed in a Web Config file and the SQLConnection object.  I am trying to specify a connection to an Oracle database in a form based VB project.  Can anyone help me out with the syntax for that?
Question by:kpbarem
    LVL 37

    Expert Comment

    by:Gerwin Jansen
    I would just look for an example like this. You mean something like that?
    LVL 76

    Expert Comment

    by:slightwv (䄆 Netminder)
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    Of course you are going to need an appropriate connection string, but you are also going to need an Oracle Client (unless you opt for the Instant Client, which I believe is a touch slower than the full blown client, but works quite nicely--in my experience).

    Once you get the prerequisites, you can simply change your SqlConnection usage to OracleConnection--likewise for *Command objects. .NET comes with the System.Data.Oracle namespace, but it is my understanding that it is deprecated in favor of Oracle's own:  Oracle.DataAccess. To get that namespace, you will need to install ODP.NET. You might also find Oracle Data Access Components (ODAC) helpful.
    LVL 76

    Accepted Solution

    OH,  Still .Net not VB6.  Sorry.

    You can still use a config file with a console style app.  You can always hard code the connection string.

    I don't have winform examples.

    Here is a console app example I have using C#

    using System;
    using System.Data;
    using Oracle.DataAccess.Client;
    using Oracle.DataAccess.Types;
    public class Bob
    	public static void Main(string[] args)
    			OracleConnection con = new OracleConnection();
     			string oradb = "Data Source=(DESCRIPTION="
              			+ "(ADDRESS=(PROTOCOL=TCP)(HOST="
              			+ "(CONNECT_DATA=(SERVICE_NAME=ORCL)));"
              			+ "User Id=scott;Password=tiger;";
    			con = new OracleConnection(oradb);
    			//con = new OracleConnection("User Id=bud;Password=bud;Data Source=bud");
    			OracleCommand cmd = new OracleCommand();
    			cmd.Connection = con;
    			cmd.CommandText = " select count(*) from dual where dummy=:myVar ";
    			OracleParameter param1 = cmd.Parameters.Add("myVar", OracleDbType.Varchar2);
    			param1.Value = 'X';
    			Console.WriteLine("My result is: " + cmd.ExecuteScalar());

    Open in new window

    LVL 74

    Expert Comment

    by:käµfm³d 👽
    You can always hard code the connection string.
    Tsk tsk tsk...


    Author Closing Comment

    This worked well thanks

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now