Fórum Criação de ODBC SQL Server via Delphi #364300
25/09/2008
0
Vi o seguinte artigo no site da devmedia :
unit unit1;
interface
uses
windows, messages, sysutils, classes, graphics, controls, forms, dialogs,
stdctrls;
type
tform1 = class(tform)
button1: tbutton;
procedure button1click(sender: tobject);
private
{ private declarations }
public
{ public declarations }
end;
var
form1: tform1;
implementation
{$r *.dfm}
procedure createodbcdriver(const
cdsnname,cexclusive,cdescription,cdatabase,cdefaultpath,cconfigsql,cdriver: string);
type
tsqlconfigdatasource = function( hwndparent: hwnd; frequest: word; lpszdriver: lpcstr;
lpszattributes: lpcstr ): bool; stdcall;
const
odbc_add_dsn = 1; // adiciona uma fonte de dados (data source)
odbc_config_dsn = 2; // configura a fonte de dados (data source)
odbc_remove_dsn = 3; // remove a fonte de dados (data source)
odbc_add_sys_dsn = 4; // adiciona um dsn no sistema
odbc_config_sys_dsn = 5; // configura o dsn do sistema
odbc_remove_sys_dsn = 6; // remove o dsn do sistema
var
pfn: tsqlconfigdatasource;
hlib: longword;
strdriver: string;
strhome: string;
strattr: string;
strfile: string;
fresult: bool;
modname: array[0..max_path] of char;
srinfo : tsearchrec;
begin
windows.getmodulefilename( hinstance, modname, sizeof(modname) );
strhome := modname;
while ( strhome[length(strhome)] <> ´\´ ) do
delete( strhome, length(strhome), 1 );
strfile := strhome + cdatabase; // teste com access (axes = access)
hlib := loadlibrary( pchar(cdefaultpath) ); // carregando para o diretório padrão
if( hlib <> null ) then
begin
@pfn := getprocaddress( hlib, pchar(cconfigsql) );
if( @pfn <> nil ) then
begin
strdriver := cdriver;
strattr := format( ´dsn=¬s´+#0+
´dbq=¬s´+0+
´exclusive=¬s´+0+
´description=¬s´+0+0,
[cdsnname,strfile,cexclusive,cdescription] );
fresult := pfn( 0, odbc_add_sys_dsn, @strdriver[1], @strattr[1] );
if( fresult = false ) then
showmessage( ´falha ao tentar criar o dsn (data source).´ );
if( findfirst( strfile, 0, srinfo ) <> 0 ) then
begin
strdriver := cdriver;
strattr := format( ´dsn=¬s´+0+
´dbq=¬s´+0+
´exclusive=¬s´+0+
´description= ¬s´+0+0+
´create_db=´¬s´´0+0,
[cdsnname,strfile,cexclusive,cdescription,strfile]);
fresult := pfn( 0, odbc_add_sys_dsn, @strdriver[1], @strattr[1] );
if( fresult = false ) then
showmessage( ´falha ao tentar criar o banco de dados´ );
end;
findclose( srinfo );
end;
freelibrary( hlib );
if fresult then
showmessage( ´banco de dados criado.´ );
end
else
begin
showmessage( ´o sistema não pode carregar a biblioteca odbccp32.dll´ );
end;
end;
procedure tform1.button1click(sender: tobject);
begin
createodbcdriver(´cludelphi dsn´,´1´,´clubedelphi´,´clubedelphi.mdb´,´odbccp32´,´sqlconfigdatasource´,´microsoft access
driver (*.mdb)´);
end;
end.
Mas preciso criar Alias no ODBC para banco de dados no SQL Server. Usando o exemplo acima, como devo proceder? Ou se alguem souber de outra maneira me avise. Obrigado.
unit unit1;
interface
uses
windows, messages, sysutils, classes, graphics, controls, forms, dialogs,
stdctrls;
type
tform1 = class(tform)
button1: tbutton;
procedure button1click(sender: tobject);
private
{ private declarations }
public
{ public declarations }
end;
var
form1: tform1;
implementation
{$r *.dfm}
procedure createodbcdriver(const
cdsnname,cexclusive,cdescription,cdatabase,cdefaultpath,cconfigsql,cdriver: string);
type
tsqlconfigdatasource = function( hwndparent: hwnd; frequest: word; lpszdriver: lpcstr;
lpszattributes: lpcstr ): bool; stdcall;
const
odbc_add_dsn = 1; // adiciona uma fonte de dados (data source)
odbc_config_dsn = 2; // configura a fonte de dados (data source)
odbc_remove_dsn = 3; // remove a fonte de dados (data source)
odbc_add_sys_dsn = 4; // adiciona um dsn no sistema
odbc_config_sys_dsn = 5; // configura o dsn do sistema
odbc_remove_sys_dsn = 6; // remove o dsn do sistema
var
pfn: tsqlconfigdatasource;
hlib: longword;
strdriver: string;
strhome: string;
strattr: string;
strfile: string;
fresult: bool;
modname: array[0..max_path] of char;
srinfo : tsearchrec;
begin
windows.getmodulefilename( hinstance, modname, sizeof(modname) );
strhome := modname;
while ( strhome[length(strhome)] <> ´\´ ) do
delete( strhome, length(strhome), 1 );
strfile := strhome + cdatabase; // teste com access (axes = access)
hlib := loadlibrary( pchar(cdefaultpath) ); // carregando para o diretório padrão
if( hlib <> null ) then
begin
@pfn := getprocaddress( hlib, pchar(cconfigsql) );
if( @pfn <> nil ) then
begin
strdriver := cdriver;
strattr := format( ´dsn=¬s´+#0+
´dbq=¬s´+0+
´exclusive=¬s´+0+
´description=¬s´+0+0,
[cdsnname,strfile,cexclusive,cdescription] );
fresult := pfn( 0, odbc_add_sys_dsn, @strdriver[1], @strattr[1] );
if( fresult = false ) then
showmessage( ´falha ao tentar criar o dsn (data source).´ );
if( findfirst( strfile, 0, srinfo ) <> 0 ) then
begin
strdriver := cdriver;
strattr := format( ´dsn=¬s´+0+
´dbq=¬s´+0+
´exclusive=¬s´+0+
´description= ¬s´+0+0+
´create_db=´¬s´´0+0,
[cdsnname,strfile,cexclusive,cdescription,strfile]);
fresult := pfn( 0, odbc_add_sys_dsn, @strdriver[1], @strattr[1] );
if( fresult = false ) then
showmessage( ´falha ao tentar criar o banco de dados´ );
end;
findclose( srinfo );
end;
freelibrary( hlib );
if fresult then
showmessage( ´banco de dados criado.´ );
end
else
begin
showmessage( ´o sistema não pode carregar a biblioteca odbccp32.dll´ );
end;
end;
procedure tform1.button1click(sender: tobject);
begin
createodbcdriver(´cludelphi dsn´,´1´,´clubedelphi´,´clubedelphi.mdb´,´odbccp32´,´sqlconfigdatasource´,´microsoft access
driver (*.mdb)´);
end;
end.
Mas preciso criar Alias no ODBC para banco de dados no SQL Server. Usando o exemplo acima, como devo proceder? Ou se alguem souber de outra maneira me avise. Obrigado.
Developer1978
Curtir tópico
+ 0
Responder
Posts
07/10/2008
Help
Já conseguiu criar o driver no ODBC?
Vc pode fazer isso utilizando o registro do windows também, veja a classe TRegistry da unit Registry.
Os drivers ficam na seguinte chave:
HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI
Vc pode fazer isso utilizando o registro do windows também, veja a classe TRegistry da unit Registry.
Os drivers ficam na seguinte chave:
HKEY_LOCAL_MACHINE\Software\ODBC\ODBC.INI
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)