WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Sample Code

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Keywords: 	 DSN less connection qDriverConnect DNS-less DSNless

Question:

I want to connect to a database with out having a DSN configured. And I have a FileDSN that has all the connection string information called 'C:\Program Files\Common Files\ODBC\Data Sources\TESTING.dsn'

Answer:

Here is an example that shows how to connect to a database without having a DSN defined, using a preexisting FileDSN to make the connection.

Also see: the qDriverCon example in the ODBC help file.

It can be done as follows....

;LINK ODBC FUNCTIONS
AddExtender("wwodb34I.dll")


;ALLOCATE A SQL ENVIRONMENT HANDLE
henv = qAllocEnv()

If henv == -1
  ;THIS RETURNS LAST CODE SET BY LAST SQL FUNCTION
  retcode = qLastCode()  
  Message("Could Not Create ODBC Environment", retcode)
  Exit
Endif


hdbc = qAllocConnect(henv) 

If hdbc == -1
  retcode = qLastCode()
  Message("qAllocConnect failed", retcode)
  Exit
Endif


;create DSN-less connection string
cConnect="FileDSN=C:\Program Files\Common Files\ODBC\Data Sources\TESTING.dsn"

ret=qDriverCon(hdbc, cConnect, 0); don't prompt user.

If ret != @qSuccess && ret != @qSuccessInfo	
  err=qError(hdbc,1)
  message("SQLDriverConnect err",err)
  exit
endif

Message("qDriverCon","Successful")
hstmt = qAllocStmt(hdbc)  

If hstmt == -1
  retcode = qLastCode()
  retcode = qERROR(hdbc,1)
  Message("qAllocStmt failed", retcode)
  Exit
Endif



;FILLS A RESULT SET W/ A LIST OF TABLE NAMES STORED IN A
;SPECIFIED DATASOURCE
retcode = qTables(hstmt, @qNull, @qNull, @qNull, @qNull) 

If (retcode != @qSuccess) && (retcode != @qSuccessInfo)
  Message("qTables failed", retcode)
  Exit
Endif

tablenamebuf = BinaryAlloc(100)

;BINDS A COLUMN IN A RESULT SET TO A VARIABLE THAT YOU NAME HERE
retcode = qBindCol(hstmt, 3, "tablenamebuf", 80) 
retcode = qBindCol(hstmt, 4, "tabletype", 80)

If (retcode != @qSuccess) && (retcode != @qSuccessInfo)
  Message("qBindCol failed", retcode)
  Exit
Endif

tables = ""

While @TRUE
  ;FETCHES A ROW OF DATA FROM A RESULT SET
  retcode = qFetch(hstmt)  

  If retcode == @qNoData Then Break

  If (retcode != @qSuccess) && (retcode != @qSuccessInfo)
    Message("qFetch failed", retcode)
    Exit
  Endif

  tablename = BinaryPeekStr(tablenamebuf, 0, BinaryEodget(tablenamebuf))

  tables = StrCat(tables, tablename, @TAB, "(", tabletype, ")", @LF)
EndWhile

BinaryFree(tablenamebuf)

;DISPLAY TABLES IN THE "SAMPLE" DATA SOURCE
Message("Tables in 'SAMPLE'", tables)



qFreeStmt(hstmt, 1)  ; SQL_DROP
qDisconnect(hdbc)
qFreeConnect(hdbc)
qFreeEnv(henv)

exit

Basically when you execute the statements...

   ;create DSN-less connection string (that points to the filedsn)
   cConnect="FileDSN=C:\Program Files\Common Files\ODBC\Data Sources\TESTING.dsn"
   ret=qDriverCon(hdbc, cConnect, 0); don't prompt user.

What happens is the system 'reads' the .dsn file to retrieve connection string information and connects to the appropriate database....



Article ID:   W14332
Filename:   DSN less connection.txt
File Created: 2003:01:21:12:11:16
Last Updated: 2003:01:21:12:11:16