Sei sulla pagina 1di 3

Add/remove ODBC data source to MS SQL Server (SQLConfigDataSo... http://www.experts-exchange.com/Programming/Languages/Pascal/Delp..

Ad d / rem ove ODBC d at a sou r ce t o M S


SQL Ser ver (SQLCon ÒgDat aSou r ce)
Delph i Pr ogr am m in g Quest ion
Asked by: snebal on 12-20-2004
Solved by: rllibby

Hello.

I would like to add and rem ove ODBC data sour ces to a MS SQL
Ser ver fr om m y pr ogram . I have the following code that alm ost
does the tr ick.

<--Begin code-->
unit m ain;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Contr ols, Form s,
Dialogs,
StdCtrls;

const
ODBC_ADD_DSN = 1; / / Add data source
ODBC_CONFIG_DSN = 2; // ConÒgure (edit) data
ODBC_REMOVE_DSN = 3; // Rem ove data source
ODBC_ADD_SYS_DSN = 4; // add a system DSN
ODBC_CONFIG_SYS_DSN = 5; // ConÒgure a system DSN
ODBC_REMOVE_SYS_DSN = 6; // remove a system DSN
ODBC_REMOVE_DEFAULT_DSN = 7; // rem ove the default DSN
SQL_MAX_MESSAGE_LENGTH = 512;

function SQLConÒgDataSource(hwndParent: HWND; fRequest:


WORD;
const lpszDr iver, lpszAttr ibutes: PChar): BOOL; stdcall;

function SQLInstallerErr or (iEr ror: WORD; pfErr or Code: PDWORD;


lpszErrorMsg: PChar; cbErr or MsgMax: WORD;
pcbErrorMsg: PWORD): Sm allint; stdcall;

type
TFor m1 = class(TFor m )
Button1: TButton;
Add/remove ODBC data source to MS SQL Server (SQLConfigDataSo... http://www.experts-exchange.com/Programming/Languages/Pascal/Delp..

Over 4 million
solutions, articles and videos
Browse Topics  Join Today Login 

Experts Exchange > Programming > Prog Languages > Pascal > Delphi > Add remove O D B C data source to M S S Q L Server S Q L Config

Data Source

var
Form 1: TFor m 1;

im plementation

{$R * .DFM}

function SQLConÒgDataSource; external 'ODBCCP32.DLL';


function SQLInstaller Er ror; external 'ODBCCP32.DLL';

procedure TFor m1.Button1Click(Sender : TObject);


var
attr: str ing;
pfErr or Code: DWORD;
er rMsg: PChar ;
begin
//Add DSN
attr := 'DSN=TEST'# 0 + 'Descr iption=Test database'# 0 +
'SERVER=DBSERVER'#0 +
'DATABASE=Test'# 0;
If not SQLConÒgDataSource(0, ODBC_ADD_DSN, 'SQL Ser ver',
PChar (attr)) Then begin
errMsg := AllocMem (SQL_MAX_MESSAGE_LENGTH);
SQLInstallerEr ror(1, @pfEr rorCode, er rMsg,
SQL_MAX_MESSAGE_LENGTH, nil);
MessageBox(0, err Msg, PChar('Cr eate System DSN Er ror # ' +
IntToStr (pfErr or Code)), 0);
FreeMem (err Msg);
end
Else
Showm essage('Done');
end;

procedure TFor m1.Button2Click(Sender : TObject);


var
attr: str ing;
Add/remove ODBC data source to MS SQL Server (SQLConfigDataSo... http://www.experts-exchange.com/Programming/Languages/Pascal/Delp..

Over 4 million
solutions, articles and videos
Browse Topics  Join Today Login 

Experts Exchange > Programming > Prog Languages > Pascal > Delphi > Add remove O D B C data source to M S S Q L Server S Q L Config

Data Source

by: rllibby Posted on 2004-12-20 at 10:14:38 ID: 12868960

Yes, it is impossible to have sql server (using odbc driver)


store the uid/pwd info. Even if you m anually add it to the
registry key, the SQLConnect(...) call will NOT utilize this
info. As I also said, this is driver speciÒc. MS Access, for
example, does allow setting this:

eg:

var attr: string;


pfErrorCode: DWORD;
errMsg: PChar;
begin

// Add DSN
attr:='DSN=TEST'#0+
'Description=Test database'#0+
'SERVER=DBSERVER'# 0+
'DATABASE=Test'# 0+
'Trusted_Connection=No'#0+
'Uid=Apples'#0+
'Pwd=Oranges'# 0+
# 0;

If not SQLConÒgDataSource(0, ODBC_ADD_DSN,


'Microsoft Access Driver (* .m db)', PChar(attr)) Then begin
errMsg := AllocMem(SQL_MAX_MESSAGE_LENGTH);
SQLInstallerError(1, @pfErrorCode, errMsg,
SQL_MAX_MESSAGE_LENGTH, nil);
MessageBox(0, errMsg, PChar('Create System DSN Error
#' + IntToStr(pfErrorCode)), 0);
FreeMem (errMsg);
end
Else

Potrebbero piacerti anche