新
新小丸子
Unregistered / Unconfirmed
GUEST, unregistred user!
以下是我写的一个组件,还在写中,这是我写的第一个组件,所以很生疏,主要实现的功能
就是让query组件可以在给出数据库类型、数据库名称、数据库用户名、数据库路径等属性值
后自动建立数据源,我知道有很多错误,请各位帮帮我好么?
unit MyQuery;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables;
type
TMyQuery = class(TQuery);
TDataBaseType=(Access,SQL,Oracle);
private
FDataBaseType:TDataBaseType;
FDataBasePath:String;
FDataBaseUser:String;
FDataBasePasswd:String;
//FDataSourceName:String;
procedure MakeDataSource;
{ Private declarations }
protected
{ Protected declarations }
public
Constructor Create(AOwner:TComponent);override;
Destructor Destroy;override;
{ Public declarations }
published
property DataBaseType:TDataBaseType Read FDataBaseType Write FDataBaseType;
property DataBasePath:String Read FDataBasePath Write FDataBasePath;
property DataBaseUser:String Read FDataBaseUser Write FDataBaseUser;
property DataBasePasswd:String Read FDataBasePasswd Write FDataBasePasswd;
//property DataSourceName:String Read FDataSourceName Write FDataSourceName;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('hamer', [TMyQuery]);
end;
constructor TMyQuery.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
//需要赋给active的激活事件的:=MakeDataSource;
end;
destructor TMyQuery.Destroy;
begin
inherited destroy;
end;
procedure TMyQuery.MakeDataSource;
var
reg:TRegistry;
thedatasource,thedatabaseUser,thedatabasepasswd,thedatabasepath:string;
bdata : array[ 0..0 ] of byte;
begin
thedatasource:=DataSource;
thedatabaseuser:=FDataBaseUser;
thedatabasepasswd:=FDataBasePasswd;
thedatabasepath:=FDataBasePath;
reg:=TRegistry.Create;
with reg do
begin
RootKey:=HKEY_LOCAL_MACHINE;
case FDataBaseType of
Access:begin
end;
SQL:begin
if not openkey('software/odbc/odbc.ini/'+thedatasource,false) then
begin
if OpenKey('Software/ODBC/ODBC.INI',True) then
WriteString( thedatasource, 'SQL Server' )
else
begin
MessageBox(Handle, '增加ODBC数据源失败!', '错误', 16);
exit;
end;
CloseKey;
if OpenKey('Software/ODBC/ODBC.INI/'+thedatasource,True) then
begin
WriteString( 'Database', thedatasource );
WriteString( 'Driver', 'C:/WINNT/SYSTEM32/SQLSRV32.dll' );
Writestring( 'LastUser', thedatabaseuser );
WriteString( 'Server', '(local)' );
end
else
begin
MessageBox(Handle, '增加ODBC数据源失败!', '错误', 16);
exit;
end;
CloseKey;
if OpenKey('Software/ODBC/ODBC.INI/odbc data sources',True) then
WriteString( thedatasource, 'SQL Server' )
else
begin
MessageBox(Handle, '增加ODBC数据源失败!', '错误', 16);
exit;
end;
CloseKey;
Application.MessageBox('数据源添加成功','正常',MB_OK);
end;
Free;
end;
Oracle:begin
end;
end;
end;
end;
end.
我会非常感谢您的,每天晚上为您祈祷[]
就是让query组件可以在给出数据库类型、数据库名称、数据库用户名、数据库路径等属性值
后自动建立数据源,我知道有很多错误,请各位帮帮我好么?
unit MyQuery;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables;
type
TMyQuery = class(TQuery);
TDataBaseType=(Access,SQL,Oracle);
private
FDataBaseType:TDataBaseType;
FDataBasePath:String;
FDataBaseUser:String;
FDataBasePasswd:String;
//FDataSourceName:String;
procedure MakeDataSource;
{ Private declarations }
protected
{ Protected declarations }
public
Constructor Create(AOwner:TComponent);override;
Destructor Destroy;override;
{ Public declarations }
published
property DataBaseType:TDataBaseType Read FDataBaseType Write FDataBaseType;
property DataBasePath:String Read FDataBasePath Write FDataBasePath;
property DataBaseUser:String Read FDataBaseUser Write FDataBaseUser;
property DataBasePasswd:String Read FDataBasePasswd Write FDataBasePasswd;
//property DataSourceName:String Read FDataSourceName Write FDataSourceName;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('hamer', [TMyQuery]);
end;
constructor TMyQuery.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
//需要赋给active的激活事件的:=MakeDataSource;
end;
destructor TMyQuery.Destroy;
begin
inherited destroy;
end;
procedure TMyQuery.MakeDataSource;
var
reg:TRegistry;
thedatasource,thedatabaseUser,thedatabasepasswd,thedatabasepath:string;
bdata : array[ 0..0 ] of byte;
begin
thedatasource:=DataSource;
thedatabaseuser:=FDataBaseUser;
thedatabasepasswd:=FDataBasePasswd;
thedatabasepath:=FDataBasePath;
reg:=TRegistry.Create;
with reg do
begin
RootKey:=HKEY_LOCAL_MACHINE;
case FDataBaseType of
Access:begin
end;
SQL:begin
if not openkey('software/odbc/odbc.ini/'+thedatasource,false) then
begin
if OpenKey('Software/ODBC/ODBC.INI',True) then
WriteString( thedatasource, 'SQL Server' )
else
begin
MessageBox(Handle, '增加ODBC数据源失败!', '错误', 16);
exit;
end;
CloseKey;
if OpenKey('Software/ODBC/ODBC.INI/'+thedatasource,True) then
begin
WriteString( 'Database', thedatasource );
WriteString( 'Driver', 'C:/WINNT/SYSTEM32/SQLSRV32.dll' );
Writestring( 'LastUser', thedatabaseuser );
WriteString( 'Server', '(local)' );
end
else
begin
MessageBox(Handle, '增加ODBC数据源失败!', '错误', 16);
exit;
end;
CloseKey;
if OpenKey('Software/ODBC/ODBC.INI/odbc data sources',True) then
WriteString( thedatasource, 'SQL Server' )
else
begin
MessageBox(Handle, '增加ODBC数据源失败!', '错误', 16);
exit;
end;
CloseKey;
Application.MessageBox('数据源添加成功','正常',MB_OK);
end;
Free;
end;
Oracle:begin
end;
end;
end;
end;
end.
我会非常感谢您的,每天晚上为您祈祷[]