帮帮忙,看看吧? (20分)

  • 主题发起人 主题发起人 新小丸子
  • 开始时间 开始时间

新小丸子

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.
我会非常感谢您的,每天晚上为您祈祷[:D]
 
Setting Active to True

Triggers the BeforeOpen event handler if one is defined for the dataset.
Sets the dataset state to dsBrowse.
Opens a cursor into the dataset.
Triggers the After Open event handler if one is defined for the dataset.
 
它的参数是一个TDataSet,就是说你也可以传TDataSet的子类给它,如TTable什么的。

procedure TForm1.ADOQuery1BeforeOpen(DataSet: TDataSet);
begin
//在这里写你要做事;
end;
或是:
procedure AllDataSetBeforeOpen(DataSet: TDataSet);
begin
ShowMessage('OK');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
adoquery1.BeforeOpen := AllDataSetBeforeOpen; //你自己定义的过程,参数必须是同一类型且数量一致
Table1.BeforeOpen := AllDataSetBeforeOpen;
end;

 
为什么没人愿意看看帮帮我呢?[:(]
 
本想给你写完,但你给的分实在太少。。。[:D]
 
不但分少,而且程序那么长,好烦人的,不如你说功能,我给你写代码了
 
帮帮忙啦,我总共才70分了[:D]
 
似乎组件的定义也有错误:
语句
TDataBaseType=(Access,SQL,Oracle);
和语句
TMyQuery = class(TQuery);
的顺序应当交换。
另外,TMyQuery = class(TQuery);后面的分号应当去掉。
 
接受答案了.
 
后退
顶部