Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > DB-Aware

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 01-29-2004, 11:01 PM
KLBADBOY KLBADBOY is offline
Member
 
Join Date: Dec 2003
Posts: 36
Default List and Get Odbc names

Hi
how are u
I'm using delphi 7 with sql server.
I want a form list all sql server driver in ODBC in a listbox / combobox.
Then When a user select a data source.
Then Adoconnection datasource set to selected data cource.
Can Anyone can help me please.
I Need Help please.
OK
Thank you in advanced.
bye
Reply With Quote
  #2  
Old 01-30-2004, 08:17 AM
MrBaseball34 MrBaseball34 is offline
Senior Member
 
Join Date: Jan 2001
Posts: 7,260
Default RE: List and Get Odbc names

Give this a try:
<tt>
const
SQL_ERROR = -1;
SQL_SUCCESS = 0;
SQL_FETCH_NEXT = 1;
SQL_FETCH_FIRST = 2;

type
EODBCError = class(Exception);

TODBCErrorEvent = procedure(Sender:TObject;odbcstate,odbcmsg:string; errcode:integer) of object;

TForm1 = class(TForm)
Button1: TButton;
ComboBox1: TComboBox;
ADOConnection1: TADOConnection;
procedure Button1Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

function SQLAllocEnv(var phenvointer):smallint; stdcall;
function SQLAllocConnect(henvointer;var phdbcointer):smallint; stdcall;
function SQLDataSources(henvointer;direction:word;szDSN char;cbDSN:word;var pbDSN:word;
szDescrchar;cbDescr:word;var pbDescr:word):smallint; stdcall;

var
Form1: TForm1;

implementation

function SQLAllocConnect; external 'ODBC32.DLL';
function SQLAllocEnv; external 'ODBC32.DLL';
function SQLDataSources; external 'ODBC32.DLL';

{$R *.dfm}

procedure GetDataSources(var DSList:TStringList);
var
dsn,descr:array[0..255] of char;
henv, hdbc: Pointer;
cbdsn,cbdescr:word;
begin
DSList.clear;
if (SQLAllocEnv(henv)<>SQL_SUCCESS) then
raise EODBCError.Create('Cannot allocate ODBC handle');

if (SQLAllocConnect(henv,hdbc)<>SQL_SUCCESS) then
raise EODBCError.Create('Cannot allocate ODBC connection');

if SQLDataSources(henv,SQL_FETCH_FIRST,dsn,sizeof(dsn ),cbDSN,descr,sizeof(descr),cbdescr)=SQL_SUCCESS then
DSList.add(strpas(dsn))
else exit;

while SQLDataSources(henv,SQL_FETCH_NEXT,dsn,sizeof(dsn) ,cbDSN,descr,sizeof(descr),cbdescr)=SQL_SUCCESS do
DSList.add(strpas(dsn));

end;

procedure TForm1.Button1Click(Sender: TObject);
var
sl: TStringList;
begin
sl := TStringList.Create;
try
GetDataSources(sl);
ComboBox1.Items.Assign(sl);
finally
sl.Free;
end;

end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
ADOConnection1.ConnectionString :=
'Provider=MSDASQL.1;Persist Security Info=False;Data Source=' +
ComboBox1.Items[Combobox1.ItemIndex];
end;
</tt>

Confusious say:
Never stand between fire hydrant and dog.

MB34
Reply With Quote
  #3  
Old 02-03-2004, 09:54 AM
MrBaseball34 MrBaseball34 is offline
Senior Member
 
Join Date: Jan 2001
Posts: 7,260
Default RE: List and Get Odbc names

So, did the reply help? If so, please accept as an answer
according to the rules of this forum.

Confusious say:
Never stand between fire hydrant and dog.

MB34
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:28 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.