Results 1 to 7 of 7

Thread: How to Create a DSN from VB...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2000
    Location
    India, Chennai
    Posts
    121

    Exclamation How to Create a DSN from VB...

    Hi Guys,
    I need to know is there any way to create a DSN for a Database (For Example Oracle or Access) using API. Is there any API for doing this. Otherwise any other method should be used. Additionally for Access we need to specify the Database Name. But for Oracle it is not needed. How should we check for the Existence of Oracle on a Machine ? First of all Is it Possible. Thnx in advance for any help.

  2. #2
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    Q171146
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Here is some code that will either create, or delete, a DSN for Oracle:

    Private Const ODBC_ADD_DSN = 1 ' Add data source
    Private Const ODBC_CONFIG_DSN = 2 ' Configure (edit) data source
    Private Const ODBC_REMOVE_DSN = 3 ' Remove data source
    Private Const vbAPINull As Long = 0& ' NULL Pointer

    Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal hwndParent As Long, ByVal fRequest As Long, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Long

    Private Sub CreateDSN(sDSN As String)
    Dim nRet As Long
    Dim sDriver As String
    Dim sAttributes As String

    sDriver = "Oracle73 Ver 2.5"
    sAttributes = "Server=pressdb.world" & Chr$(0)
    sAttributes = sAttributes & "DESCRIPTION=" & sDSN & Chr$(0)
    sAttributes = sAttributes & "DSN=" & sDSN & Chr$(0)
    sAttributes = sAttributes & "DATABASE=DB" & Chr$(0)
    sAttributes = sAttributes & "UID=Waty" & Chr$(0)
    sAttributes = sAttributes & "PWD=myPassword" & Chr$(0)

    DBEngine.RegisterDatabase "kiki", "Oracle73 Ver 2.5", True, sAttributes

    nRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, sDriver, sAttributes)
    End Sub

    Private Sub DeleteDSN(sDSN As String)
    Dim nRet As Long
    Dim sDriver As String
    Dim sAttributes As String

    sDriver = "Oracle73 Ver 2.5"
    sAttributes = sAttributes & "DSN=" & sDSN & Chr$(0)

    nRet = SQLConfigDataSource(vbAPINull, ODBC_REMOVE_DSN, sDriver, sAttributes)

    End Sub

  4. #4
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    Perhaps you should look at connecting using a DSN-Less connection, and storing the configuration information in the registry.

    You get to use OLE-DB then which with ADO has far superior performance.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2000
    Location
    India, Chennai
    Posts
    121

    Exclamation Can u tell me How should I use a OLE-DB Connection...

    Hi yrwyddfa,
    Can u give me a example on how should I use a OLE-DB Connection. Thnx in Advance.

  6. #6
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Ora

    I can't test this, because I haven't got Oracle, but I guess it would be something like this:

    VB Code:
    1. Function GetCn( _
    2.                 ByVal sServer As String, _
    3.                 ByVal sDatabase As String, _
    4.                 ByVal sUID As String, _
    5.                 ByVal sPassword As String _
    6.                 ) _
    7.                 As ADODB.Connection
    8.                    
    9.                    
    10.     Dim oCn As ADODB.Connection
    11.     Dim sCn As String
    12.    
    13.     Set oCn = CreateObject("ADODB.Connection")
    14.    
    15.     oCn.Provider = "MSDAORA"
    16.     oCn.Properties("User ID") = "Waty"
    17.     oCn.Properties("Data Source") = "pressdb.world"
    18.     oCn.Properties("Initial Catalog") = "DB"
    19.    
    20.     oCn.Open
    21.    
    22.     If oCn.State <> adStateOpen Then
    23.         'raise errors here
    24.         Set GetCn = Nothing
    25.     Else
    26.         Set GetCn = oCn
    27.     End If
    28.    
    29. End Function

    I hope this helps a little.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2000
    Location
    India, Chennai
    Posts
    121

    Cool Thnx yrwyddfa, I'll try it and mail you...

    Hi yrwyddfa,
    I got ur code and mail u after I got it working. Thnx for ur help.
    Regards,
    Ramanan.

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