Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO ADD DSN IN ODBC USING VFP

Status
Not open for further replies.

remsor

Programmer
Nov 5, 2002
101
PH
Hello Vfp Experts,

Anybody have an experience on how to insert/add dsn in OBDC using vfp6.

thanks
 
Remsor

One way would to create a textfile using FCREATE() and using FPUTS() inserting the appropriate init string and changing the extention of the textfile to ".udl"
Here is an example (note the connection string should be a valid one)
Code:
cFile = Sys(2023) + Sys(3) + '.UDL'
If File(m.cFile)
    Erase (m.cFile)
Endif
nFh = Fcreate(m.cFile)
If m.nFh < 0
    Wait &quot;Can't create a temporary file for the email&quot; Window Nowait
    Return
Endif
= Fputs(m.nFh,'[oledb]')
= Fwrite(m.nFh,&quot;Provider=MSDASQL.1;Persist Security Info=False;Data Source=MS Access Database;Mode=ReadWrite&quot;)
= Fflush(m.nFh)
= Fclose(m.nFh)
Run /N2 Start &cFile
Return .T.
Endfunc


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Remsor,

I've always just used a udl file in the past (similar to the approach Mike outlined). So, your question intrigued me since there has to be a way to do it from code. As I suspected it is a pretty straight forward and simple API call. I've worked up a little program below to show how it is done. Cut-n-paste the code below into a prg file and run it from within VFP.


PUBLIC oForm
oForm = CREATEOBJECT(&quot;clsvfpdsn&quot;)
oForm.show()

DEFINE CLASS clsvfpdsn AS form

Top = 0
Left = 0
Height = 92
Width = 309
DoCreate = .T.
Caption = &quot;VFP CREATED DSN&quot;
WindowType = 1
Name = &quot;Form1&quot;
AutoCenter = .T.

ADD OBJECT command1 AS commandbutton WITH ;
Top = 45, ;
Left = 37, ;
Height = 27, ;
Width = 100, ;
Caption = &quot;Add DSN&quot;, ;
Name = &quot;Command1&quot;

ADD OBJECT command2 AS commandbutton WITH ;
Top = 45, ;
Left = 171, ;
Height = 27, ;
Width = 100, ;
Caption = &quot;Remove DSN&quot;, ;
Name = &quot;Command2&quot;

ADD OBJECT check1 AS checkbox WITH ;
Top = 19, ;
Left = 37, ;
Height = 17, ;
Width = 82, ;
AutoSize = .T., ;
BackStyle = 0, ;
Caption = &quot;Silent Mode&quot;, ;
Value = 1, ;
Name = &quot;Check1&quot;

PROCEDURE Load
DECLARE INTEGER SQLConfigDataSource IN odbccp32;
INTEGER hwndParent,;
INTEGER fRequest,;
STRING lpszDriver,;
STRING lpszAttributes
ENDPROC

PROCEDURE command1.Click
#DEFINE ODBC_ADD_DSN 1
*!* #DEFINE ODBC_CONFIG_DSN 2
*!* #DEFINE ODBC_REMOVE_DSN 3

Local nReturnValue, cDriver, cAttribs

cDriver = &quot;SQL Server&quot;
cAttribs = &quot;SERVER=SomeServer&quot; + Chr(0)
cAttribs = cAttribs + &quot;DESCRIPTION=Temporary DSN created with VFP code&quot; + Chr(0)
cAttribs = cAttribs + &quot;DSN=VFP_CREATED_DSN&quot; + Chr(0)
cAttribs = cAttribs +&quot;DATABASE=pubs&quot; + Chr(0)

IF thisform.check1.value = 0
nReturnValue = SQLConfigDataSource(thisform.hwnd, ODBC_ADD_DSN, cDriver, cAttribs)
ELSE
nReturnValue = SQLConfigDataSource(0, ODBC_ADD_DSN, cDriver, cAttribs)
ENDIF

If nReturnValue = 1
Messagebox(&quot;VFP_CREATED_DSN Created&quot;,64,&quot;SUCCESS&quot;)
Else
Messagebox(&quot;Creation of VFP_CREATED_DSN Failed&quot;,64,&quot;FAILURE&quot;)
Endif
ENDPROC

PROCEDURE command2.Click
#DEFINE ODBC_REMOVE_DSN 3
*!* #DEFINE ODBC_ADD_DSN 1
*!* #DEFINE ODBC_CONFIG_DSN 2

Local nReturnValue, cDriver, cAttribs

cDriver = &quot;SQL Server&quot;
cAttribs = &quot;DSN=VFP_CREATED_DSN&quot; + Chr(0)

IF thisform.check1.value = 0
nReturnValue = SQLConfigDataSource(thisform.hwnd, ODBC_REMOVE_DSN, cDriver, cAttribs)
ELSE
nReturnValue = SQLConfigDataSource(0, ODBC_REMOVE_DSN, cDriver, cAttribs)
ENDIF

If nReturnValue = 1
Messagebox(&quot;VFP_CREATED_DSN Deleted&quot;,64,&quot;SUCCESS&quot;)
Else
Messagebox(&quot;Deletion of VFP_CREATED_DSN Failed&quot;,64,&quot;FAILURE&quot;)
Endif
ENDPROC

ENDDEFINE



Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
Pre-existing FAQ I found after submitting my own:

faq184-1538

New FAQ I just created for the code I posted in this thread:

faq184-4064


Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top