![]() |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 phenv ointer):smallint; stdcall;function SQLAllocConnect(henv ointer;var phdbc ointer):smallint; stdcall;function SQLDataSources(henv ointer;direction:word;szDSN char;cbDSN:word;var pbDSN:word;szDescr char;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 |
|
#3
|
|||
|
|||
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
|
|