Програмное создание нового DSN


type
TSQLConfigDataSource =
function(hwndParent: Integer;
fRequest: Integer;
lpszDriverString: string;
lpszAttributes: string): Smallint; stdcall;

function SQLConfigDataSource(hwndParent: Integer; fRequest: Integer;
lpszDriverString: string; lpszAttributes: string): Integer; stdcall;
var
func: TSQLConfigDataSource;
OdbccpHMODULE: HMODULE;

begin
OdbccpHMODULE := LoadLibrary(′c:WINDOWSSYSTEModbccp32.dll′);
if OdbccpHMODULE = 0 then
raise Exception.Create(SysErrorMessage(GetLastError));
func := GetProcAddress(OdbccpHMODULE, PChar(′SQLConfigDataSource′));
if @func = nil then
raise Exception.Create(′Error Getting adress for SQLConfigDataSource′ +
SysErrorMessage(GetLastError));
Result := func(hwndParent, fRequest, lpszDriverString, lpszAttributes);
FreeLibrary(OdbccpHMODULE);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if SQLConfigDataSource(0, 1, ′Microsoft Excel Driver (*.xls)′,
Format(′DSN=%s;DBQ=%s;DriverID=790′, [′MyDSNName′,
′c: emp emp.xls′])) <> 1 then
ShowMessage(′Cannot create ODBC alias′);
end;



Опубликовал admin
11 Мар, Вторник 2008г.



Программирование для чайников.