Results 1 to 20 of 20

Thread: connection w/ Oracle tables

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Sharpsville, PA
    Posts
    135

    connection w/ Oracle tables

    Hello Again,

    I got past the regsvcs issue, but have a new one. How does one set up a data connection/command to Oracle data as opposed to
    MS SQL. I have no smaples to go by, I winged itm, but it blew up.
    (The connection is in a DLL) I was using Miscrosoft OLE driver for
    Oracle, not sure if that is even right. Isn't there a .net version? Any help samples appreciated
    Thanks
    Tom

  2. #2
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Exclamation

    I'd like to see an example as well.

    My attempts have all failed.
    ~Peter


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Sharpsville, PA
    Posts
    135

    Thumbs up Oracle and VB.NET

    I found out how to do it by chance:

    (Doesn't that suck; such "hi-tech" stuff, but goodluck trying
    to find solid documentation)

    This example will read the first row and update a field:


    Dim s_success As String
    Dim s_connection As String
    Dim s_command As String

    Dim o_connection As OleDb.OleDbConnection
    Dim o_adapter As OleDb.OleDbDataAdapter
    Dim o_builder As OleDb.OleDbCommandBuilder

    Dim o_dataset As DataSet
    Dim o_datatable As DataTable
    Dim o_datatablerow As DataRow

    s_connection = "Provider=MSDAORA.1;Password=XXXX;User ID=XXX;Data Source=XXX;Persist Security Info=True"
    s_command = "select * from tablename"

    o_connection = New OleDb.OleDbConnection(s_connection)
    o_adapter = New OleDb.OleDbDataAdapter(s_command, o_connection)
    o_builder = New OleDb.OleDbCommandBuilder(o_adapter)

    o_dataset = New DataSet()
    o_adapter.Fill(o_abr, "tablename")

    o_datatable = o_dataset.Tables("tablename")
    o_datatablerow = o_datatable.Rows(0)
    o_datatablerow("fieldname") = "GX"
    o_adapter.Update(o_datatable)



    The "trick" i discovered (by a guy trying to do the same to db2 at Wrox's site) is that you have to reference the table in the o_adapter.Fill(o_abr, "tablename"). In SQL you do not.

    Good Luck
    Tom

  4. #4
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Unhappy

    MISSING: you didn't say what o_abr was!

    I can't even try the code until i know that that is supposed to be. Sorry.
    ~Peter


  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Sharpsville, PA
    Posts
    135

    OOPS

    Sorry,
    o_abr should be changed to o_dataset;

    I replaced my propriatary table names w/generic and
    I missed a line!

  6. #6
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    I managed to use your code, along with some other knowledge i had to create this tighter way to get data from a table:
    VB Code:
    1. Private Sub btnSelectData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectData.Click
    2.         Dim sEntry As String = "Plasma"
    3.         Dim DS As DataSet = MakeConnectionOracle("SELECT * FROM ContactManagerCodes", sEntry)
    4.         If DS.Tables(sEntry).Rows.Count > 0 Then
    5.             Dim sText As String = DS.Tables(sEntry).Columns(0).Caption & ": " & DS.Tables(sEntry).Rows(0).Item(0) & vbCrLf
    6.             sText = sText & DS.Tables(sEntry).Columns(1).Caption & ": " & DS.Tables(sEntry).Rows(0).Item(1) & vbCrLf
    7.             sText = sText & DS.Tables(sEntry).Columns(2).Caption & ": " & DS.Tables(sEntry).Rows(0).Item(2) & vbCrLf
    8.             MsgBox(sText, MsgBoxStyle.Information, "Found")
    9.         Else
    10.             MsgBox("No records found!", MsgBoxStyle.Critical, "-")
    11.         End If
    12.     End Sub
    13.  
    14.     Public Const ORACLE_DB_CONNECT_STRING = "Provider=MSDAORA.1;Password=HaHaHa;User ID=HoHoHo;Data Source=SISSOL.WORLD;Persist Security Info=True"
    15.  
    16.     Public Function MakeConnectionOracle(ByVal sSQL As String, ByVal sTempDataSetName As String) As DataSet
    17.         'Creates connection to database and gets a dataset of values
    18.         Dim Conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ORACLE_DB_CONNECT_STRING)
    19.         Dim xAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sSQL, Conn)
    20.         Dim DS As DataSet = New DataSet()
    21.         'Sends back a dataset object full of data from the SQL statement
    22.         xAdapter.Fill(DS, sTempDataSetName)
    23.         Return DS
    24.     End Function
    Last edited by MrGTI; Aug 28th, 2002 at 09:20 AM.
    ~Peter


  7. #7
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs down

    I'm within a hair of having the INSERT code working. I'm having a tiny problem, but i'm trying to solve it.

    It's sad how hard it is to get it to work with Oracle. All my SS2K code worked like a charm. But everything i do with Oracle is like a constant battle. But hey .... whatca gonna do right? Work uses Oracle, and i can't change that!
    ~Peter


  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Sharpsville, PA
    Posts
    135

    Cool!

    Yeah,
    mine is very rudimentary, but based on my rather limited experience, was tickled to get it to work. Yours look interseting.
    I will try it out

    Thanks
    Tom

  9. #9
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    Hope it works out for you Tom Yasnowski.

    I should have mentioned that (of course), my project has this at the top:
    VB Code:
    1. Imports System.Data
    2. Imports System.Data.SqlClient
    3. Imports System.Data.OleDb
    ~Peter


  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Sharpsville, PA
    Posts
    135

    Oracle

    Yep Got it!

    Yeah, All these tutorials/sample use SQL. I do not know why
    they ignore arguably the biggest DB in terms of market share,Oracle. I guess its a lot easier to just grab "Northwind"--
    Laziness IMHO.

    Tom

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Sharpsville, PA
    Posts
    135

    sEntry = "Plasma"?

    GTI,

    I realized that I did not change the sEntry = "Plasma" parameter. It still worked anyway (Good- I think), what does this this setting mean/do?

    Thanks
    Tom

  12. #12
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    Good to hear you also got it working.

    The variable sEntry is basically a "name" for the set of data that it retrieves. You can populate the DataSet with multiple sets of data - you just need to name each one. In my case i only use the DataSet once, so i give it a basic/useless name.

    As for SS2K vs Oracle - yes Oracle is still the biggest. But Microsoft has made SS2K just as powerful as Oracle, so it's only a matter of time before Oracle loses market share. Yes, i personally enjoy using SS2K way more than Oracle.
    ~Peter


  13. #13
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up Insert Statement

    And here is the INSERT statement i promised:
    VB Code:
    1. Const INSERT_SQL = "INSERT INTO STU_Programs24HR (UniqueID, StudentID) VALUES (?, ?)"
    2.         Dim Conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ORACLE_DB_CONNECT_STRING)
    3.         Dim Cmd As New OleDbCommand()
    4.  
    5.         With Cmd
    6.             .CommandText = INSERT_SQL
    7.             .Connection = Conn
    8.  
    9.             .Parameters.Clear()
    10.             .Parameters.Add(New OleDb.OleDbParameter("@p1", OleDbType.Numeric, 10))
    11.             .Parameters.Add(New OleDb.OleDbParameter("@p2", OleDbType.VarChar, 9))
    12.             .Parameters("@p1").Value = 123
    13.             .Parameters("@p2").Value = "working"
    14.             '.Parameters.Add("@p1", OleDbType.VarChar, 9).Value = "Mmmm"
    15.  
    16.             .Connection.Open()
    17.             Try
    18.                 .ExecuteNonQuery()
    19.                 MsgBox("done")
    20.             Catch Exp As OleDbException
    21.                 MsgBox(Exp.ErrorCode & " " & Exp.Message)
    22.             End Try
    23.             .Connection.Close()
    24.         End With
    Special thanks to my buddy Stew how helped me sort out a few minor glitches i had in getting this to work.

    You'll need to use the ORACLE_DB_CONNECT_STRING constant that i created in the previous postings. As well, the line that is commented out is an alternate way of doing it.

    And of course, .... you can use this to create an UPDATE statement as well.
    ~Peter


  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Sharpsville, PA
    Posts
    135

    Cooler Still!

    GTI,
    Thanks for the sample.

    But I was wondering what these parameters do for you? (don't have the time right now to "play around")

    .Parameters.Clear()
    .Parameters.Add(New OleDb.OleDbParameter("@p1", OleDbType.Numeric, 10))
    .Parameters.Add(New OleDb.OleDbParameter("@p2", OleDbType.VarChar, 9))
    .Parameters("@p1").Value = 123
    .Parameters("@p2").Value = "working"


    As for the "plasma" query--thanks for clearing that up.
    all in all, it seems to me the sample I originally gave you is not reflected at all in the "plasma" sample other than the standard OLE connections. So I take it you got mine to work which gave you enough of a push to try a totally different approach?

    Tom

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Sharpsville, PA
    Posts
    135

    Red face Doh!

    GTI
    Ok, I see where my stuff is reflected in your sample. Boy I must
    come off even more stupid than I really am sometimes


    I didn't look at it close enough (doing many things at once today)


    I still do not know what those parms are about; they wern't used
    to fill the values of the insert statement where they?


  16. #16
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Post

    In my insert statement, you will notice two question marks where there should have been values.

    When you add a parameter to the Command object (called Cmd in my example) it is basically an object that will recieve a value. The first parameter is called @p1. You could have called it @UniqueID if you wanted too. You can also see where i assign a value to the parameter.

    Once you peform the insert ( Cmd.ExecuteNonQuery() ) you can then just assign the parameter a new value, and then execute the insert statement again.

    This code is a condensed version of what i normally do. In most situations i create all the parameters first, and then perform a loop where i assign value to the parameters and execute the SQL statement.
    ~Peter


  17. #17
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    Well after all that you could download the new .net provider for oracle. http://msdn.microsoft.com/downloads/...mpositedoc.xml

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Sharpsville, PA
    Posts
    135

    Parm pass

    GTI,

    I thought that was what you were doing, but I never saw it before. Wasn't sure if those "?" where reserved or were just
    your way of saying "whatever value you want". In VB6 I
    built the insert sql statement with the values imbedded via
    variables: sql_statement = "insert into xxx..,' " & str_value & " ' "
    Your method looks a lot more practical.

    Thanks

    Musician,

    Thanks for the link;But, Assuming your link provides the samples we have discussed, isn't it fun to watch people reinvent the wheel?

    Tom

  19. #19
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    I noticed you use the OleDbConnection object to connect to oracle. I would recommend using the new Oracle managed provider for .NET like Musician mentioned. There is a lot of performance to gain, because the OleDbConnection needs COM interop that slows things down.

    If you still want to use the OleDbConnection object; this is from the developers guide of the Oracle OLEDB provider:

    Using the OLEDB.NET Attribute in a Connection String
    When using OraOLEDB with the OLE DB .NET Data Provider, the OLEDB.NET
    connection attribute must be set to True as shown in the following examples:
    // in VB.NET
    Dim con As New OleDbConnection()
    con.ConnectionString = "Provider=OraOLEDB.Oracle;User Id=scott;" & _
    "Password=tiger;Data Source=Oracle;OLEDB.NET=True;"
    con.Open
    // in C#
    ...
    OleDbConnection con = new OleDbConnection();
    con.ConnectionString = "Provider=OraOLEDB.Oracle;User Id=scott;" +
    "Password=tiger;Data Source=Oracle;OLEDB.NET=true;"
    con.Open();
    ...

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Sharpsville, PA
    Posts
    135

    Oracle

    I have Oracle 8.0, This is for 8i I understand..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width