dynamically create database access
Posted: Mon 11 Jun 2012 11:21
how I can create runtime databases and ms access tables?
Discussion forums for open issues and questions concerning database tools, data access components and developer tools from Devart
https://forums.devart.com/
Code: Select all
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, UniProvider, ODBCUniProvider, AccessUniProvider, DB, DBAccess,
Uni;
type
TForm1 = class(TForm)
AccessUniProvider1: TAccessUniProvider;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TSQLConfigDataSource = function(hwndParent: Integer; fRequest: Integer;
lpszDriverString: string;
lpszAttributes: string): Smallint; stdcall;
var
Form1: TForm1;
implementation
{$R *.dfm}
Function SQLConfigDataSource(hwndParent: Integer; fRequest: Integer;
lpszDriverString: string; lpszAttributes: string): Integer; stdcall;
var
func : TSQLConfigDataSource;
ODBCHModule: HMODULE;
begin
ODBCHModule := LoadLibrary('odbccp32.dll');
if ODBCHModule = 0 then
raise Exception.Create(SysErrorMessage(GetLastError));
func := GetProcAddress(ODBCHModule,PChar('SQLConfigDataSource'));
if @func = nil then
raise Exception.Create('Error Getting adress for SQLConfigDataSource' + SysErrorMessage(GetLastError));
Result := func(hwndParent, fRequest, lpszDriverString, lpszAttributes);
FreeLibrary(ODBCHModule);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
UniConnetion: TUniConnection;
DatabaseName: String;
begin
UniConnetion := TUniConnection.Create(nil);
UniConnetion.ProviderName := 'Access';
DatabaseName := 'd:\MyAccessDB.mdb';
SQLConfigDataSource(0, 1, 'Microsoft Access Driver (*.mdb)','CREATE_DB="' + DatabaseName + '"');
UniConnetion.Database := DatabaseName;
UniConnetion.Connect;
UniConnetion.ExecSQL('CREATE TABLE MyTable (Field1 CHAR)',[]); //'CREATE INDEX ...', etс.
UniConnetion.Free;
end;
end.