Arsip: Ubah ODBC (MS-Acess, MySQL, MS-SQL Server) dengan Delphi


by onsir in Articles more 13 years ago 4639
Ubah ODBC (MS-Acess, MySQL dan MS-SQL Server) dari Delphi.
Kode ini digunakan untuk merubah konfigurasi ODBC dari Delphi. Kode ini dapat berguna bila anda menggunakan laporan dari Crystal Report yang menggunakan koneksi ODBC, dari Server atau Database yang berbeda.
1. Buat aplikasi baru
2. Create New Unit, lalu ketik/copy kode dibawah ini.
unit OdbcConfig;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables;
function SQLConfigDataSource(HwndParent:THandle; fRequest:integer; Driver, Attribute:PChar):integer;stdcall;external 'ODBCCP32.DLL';
type
TODBC_Command = (ODBC_NO_COMMAND, ODBC_ADD_DSN, ODBC_CONFIG_DSN, ODBC_REMOVE_DSN,
ODBC_ADD_SYS_DSN, ODBC_CONFIG_SYS_DSN, ODBC_REMOVE_SYS_DSN);
type
TDSN = class
public
{ untuk DB MS-Acess
class function CreateMSAccessDSN(DSNName, path:String):integer;
class function DeleteMSAccessDSN(DSNName, path:String):integer;
}
{ untuk MYSQL
class function CreateMySQLDSN(DSNName, Server,Database,User,Password,Option:String):integer;
class function DeleteMySQLDSN(DSNName, Server,Database,User,Password,Option:String):integer;
}
//untuk MSSQL Server
       class function CreateMSSQLDSN(DSNName,Server,Database:String):integer;
class function DeleteMSSQLDSN(DSNName,Server,Database:String):integer;
end;
implementation
{
---- untuk DB MS-Acess --
class function TDSN.CreateMSAccessDSN(DSNName, path:String):integer;
begin
Result := SQLConfigDataSource(0, Ord(ODBC_ADD_SYS_DSN), 'Microsoft Access Driver (.mdb)',
Pchar('DSN='+DSNName+#0+'FIL=MS ACCESS'+#0+'DBQ='+path+#0));
end;
class function TDSN.DeleteMSAccessDSN(DSNName, path:String):integer;
begin
Result := SQLConfigDataSource(0, Ord(ODBC_REMOVE_SYS_DSN), 'Microsoft Access Driver ( .mdb)',
Pchar('DSN='+DSNName+#0+'FIL=MS ACCESS'+#0+'DBQ='+path+#0));
end;
}
//-------------------------------------------------------------------------
{----untuk MYSQL
class function TDSN.CreateMySQLDSN(DSNName, Server,Database,User,Password,Option:String):integer;
begin
Result := SQLConfigDataSource(0, Ord(ODBC_ADD_SYS_DSN), 'MySQL',
Pchar('DSN='+DSNName+#0+'Server='+Server+#0+'Database='+Database+#0+'User='+User+#0+'Password='+Password+#0+'Option='+Option+#0));
end;
class function TDSN.DeleteMySQLDSN(DSNName, Server,Database,User,Password,Option:String):integer;
begin
Result := SQLConfigDataSource(0, Ord(ODBC_REMOVE_SYS_DSN), 'MySQL',
Pchar('DSN='+DSNName+#0+'Server='+Server+#0+'Database='+Database+#0+'User='+User+#0+'Password='+Password+#0+'Option='+Option+#0));
end;
}
//untuk MSSQL Server
class function TDSN.CreateMSSQLDSN(DSNName,Server,Database:String):integer;
begin
Result := SQLConfigDataSource(0, Ord(ODBC_ADD_SYS_DSN), 'SQL Server',
Pchar('DSN='+DSNName+#0+'Server='+Server+#0+'Database='+Database+#0));
end;
class function TDSN.DeleteMSSQLDSN(DSNName,Server,Database:String):integer;
begin
Result := SQLConfigDataSource(0, Ord(ODBC_REMOVE_SYS_DSN), 'SQL Server',
Pchar('DSN='+DSNName+#0+'Server='+Server+#0+'Database='+Database+#0));
end;
end.
3.    Save unit tsb.
4.    di unit1 ketikan/copy  kode berikut
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls,Registry, Crystal_TLB;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button7: TButton;
Button8: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses OdbcConfig;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
//odbcConfig.TDSN.CreateMSAccessDSN('dbdemosodbc','d:/dbdemos');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//odbcconfig.TDSN.DeleteMSAccessDSN('dbdemosodbc','d:/dbdemos');
//odbcConfig.TDSN.CreateMSAccessDSN('dbdemosodbc','d:/xtreme2');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
//odbcconfig.TDSN.CreateMySQLDSN('Bintasy','ServerLK','LHT','root','','3');
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
//odbcconfig.TDSN.DeleteMySQLDSN('Bintasy','ServerLK','LHT','root','','3');
//odbcconfig.TDSN.CreateMySQLDSN('Bintasy','ServerLK','BKA','root','','3');
end;
procedure TForm1.Button5Click(Sender: TObject);
Var Registry: TRegistry;
begin
odbcconfig.TDSN.CreateMSSQLDSN('B2','.','bumick');
Registry:=TRegistry.Create;
Registry.RootKey:=HKEY_LOCAL_MACHINE;
Registry.OpenKey('SOFTWARE\\\\ODBC\\ODBC.INI\\\\B2',False);
Registry.WriteString('LastUser','sa');
Registry.CloseKey;
Registry.Free;
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
odbcconfig.TDSN.DeleteMSSQLDSN('B2','.','bumick');
end;
end.
Syntax Highlighted with https://pascal-id.org/syntax


Note: Penggunaan TRegistry utk ODBC MS-SQL Server. Karena bila tidak menggunakan TRegistry tsb, ODBC yang tercipta, isi dari LastUser akan terisi nama Administrators Windows. Bukan nama login ‘sa’ (user default). Nama ‘sa’ dapat kita ganti dengan nama login yang lain, bila kita sudah membuat nama login di SQL Server.

Terima kasih untuk Maling list Delphi dan saudara Wisnu.
Local Business Directory, Search Engine Submission & SEO Tools FreeWebSubmission.com SonicRun.com