How To Use “DSN-Less” ODBC Connections with RDO
Symptoms
With Microsoft Visual Basic versions listed above, you can specify yourODBC driver and server in your connect string when using RDO (Remote DataObjects) and DAO (Data Access Objects). This eliminates the need to set upa DSN (Data Source Name). This is called a “DSN-Less” ODBC connectionbecause you do not need to set up a DSN in order to access your ODBCdatabase server.
To do this, you specify a “driver=” parameter in your connect property.The following three examples show how this is done with the SQL Server,Access, and Oracle ODBC drivers:
‘Microsoft SQL Server ODBC Driver examplecnstr = “driver={SQL Server};server=myserver;” & _”database=pubs;uid=<username>;pwd=<strong password>”cn.Connect = cnstr’Microsoft Access ODBC Driver example (version 2.x)cnstr = “Driver={Microsoft Access Driver (*.mdb)};” & _”Dbq=c:\program files\devstudio\vb\biblio.mdb;” & _”Uid=Admin; Pwd=”cn.Connect = cnstr’Microsoft ODBC Driver for Oracle examplecnstr = “Driver={Microsoft ODBC Driver for Oracle};” & _”Server=OracleServer.world; Uid=demo; Pwd=demo”‘ Note that 1.0 version of the MicrosoftOracle driver used’ “ConnectString” notation instead of “Server”
NOTE: The driver name must be surrounded by curly brackets. For example:
“{SQL Server}”
The following information is taken from Visual Basic Books Online:
The connect string contains a series of semi-colon-delimitedarguments as defined by the ODBC interface – including theODBC driver itself. That is, all ODBC drivers have specificargument requirements so you should consult the documentationincluded with the driver for specific information. Thisconnect string is passed to the ODBC API SQLDriverConnectfunction along with the hEnv for the associated rdoEnvironmentobject.
Resolution
If you do want to set up a DSN, you can use the following methods:
Manually with the ODBC Admin utility(Odbcad32.exe).Through code with the RDO rdoRegisterDataSource method.Through code with the DAO RegisterDatabase method.Through code with the ODBC API SQLConfigDatasource API function.
Sample ProgramThe following RDO example uses a “DSN-less” ODBC connection so you do notneed to set up a DSN with the ODBC Admin utility beforehand.
Start a new project in Visual Basic. Form1 is created by default.Add a CommandButton to Form1, Command1 by default.Paste the following code into the code window of Form1.
Note You must change UID =<username> and PWD =<strong password> to the correct values before you run this code. Make sure that UID has the appropriate permissions to perform this operation on the database.
Private Sub Command1_Click()Dim Cn As New rdoConnection’creatable rdoConnectionDim Qr As New rdoQuery’creatable rdoQueryDim Rs As rdoResultset’pointer to rdoResultsetDim cnstr As String’hold connection infocnstr = “driver={SQL Server};server=myserver;” & _”database=pubs;uid=<username>;pwd=<strong password>”Cn.Connect = cnstrCn.CursorDriver = rdUseClientBatchCn.EstablishConnection Prompt:=rdDriverNoPromptSet Qr.ActiveConnection = CnQr.SQL = “Select * From Authors”Set Rs = Qr.OpenResultset(Type:=rdOpenKeyset, _LockType:=rdConcurBatch)Debug.Print Rs(0), Rs(1), Rs(2)End Sub Note that you must change your DRIVER, SERVER, DATABASE, UID, and PWDparameters in the Connect method. You also need to modify the SQLstatement contained in the Command1_Click event to match your own SQLdata source.Start the program or press the F5 key.Click the Command1 button to create an rdoResultset and display thefirst row of data in the debug window.

Leave a Reply