谁能解释一下下面的程序是在做什么,每一行程序的作用哟,用中文,嘿嘿,我知道在在做什么,可是还没怎么看明白(119分)

  • 主题发起人 主题发起人 第三个帐号了
  • 开始时间 开始时间

第三个帐号了

Unregistered / Unconfirmed
GUEST, unregistred user!
unit OpenSQLServer;

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls,
Buttons, ComCtrls, ExtCtrls, Dialogs;

type
TSQLConnection = record
ServerName : widestring;
DatabaseName : wideString;
UserName : widestring;
Password : widestring;
end;

TOpenSQLServerForm = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
cboServers: TComboBox;
Label1: TLabel;
Label2: TLabel;
cboDatabases: TComboBox;
GroupBox1: TGroupBox;
rbIntegratedSecurity: TRadioButton;
rbLoginInfo: TRadioButton;
ledUserName: TLabeledEdit;
ledPassword: TLabeledEdit;
TestConButton: TBitBtn;
btnOk: TBitBtn;
btnCancel: TBitBtn;
procedure rbLoginInfoClick(Sender: TObject);
procedure rbIntegratedSecurityClick(Sender: TObject);
procedure cboServersClick(Sender: TObject);
procedure btnOKClick(Sender: TObject);
procedure TestConButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
private
SC : TSQLConnection;
function GetConnStr: widestring;

procedure DatabasesOnServer(Databases : TStrings);

property ConnStr : widestring read GetConnStr;
public
class function Execute : widestring;
end;

var
OpenSQLServerForm: TOpenSQLServerForm;

implementation
{$R *.dfm}
uses DB, ADODB, Variants, ActiveX, ComObj, AdoInt, OleDB;


class function TOpenSQLServerForm.Execute: widestring;
begin
with TOpenSQLServerForm.Create(nil) do
try
ShowModal;
if ModalResult = mrOK then
Result := ConnStr
else
Result := '';
finally
Free;
end;
end;



procedure ListAvailableSQLServers(Names : TStrings);
var
RSCon: ADORecordsetConstruction;
Rowset: IRowset;
SourcesRowset: ISourcesRowset;
SourcesRecordset: _Recordset;
SourcesName, SourcesType: TField;

function PtCreateADOObject(const ClassID: TGUID): IUnknown;
var
Status: HResult;
FPUControlWord: Word;
begin
asm
FNSTCW FPUControlWord
end;
Status := CoCreateInstance(
CLASS_Recordset,
nil,
CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER,
IUnknown,
Result);
asm
FNCLEX
FLDCW FPUControlWord
end;
OleCheck(Status);
end;
begin
SourcesRecordset := PtCreateADOObject(CLASS_Recordset) as _Recordset;
RSCon := SourcesRecordset as ADORecordsetConstruction;
SourcesRowset := CreateComObject(ProgIDToClassID('SQLOLEDB Enumerator')) as ISourcesRowset;
OleCheck(SourcesRowset.GetSourcesRowset(nil, IRowset, 0, nil, IUnknown(Rowset)));
RSCon.Rowset := RowSet;
with TADODataSet.Create(nil) do
try
Recordset := SourcesRecordset;
SourcesName := FieldByName('SOURCES_NAME'); { do not localize }
SourcesType := FieldByName('SOURCES_TYPE'); { do not localize }
Names.BeginUpdate;
try
while not EOF do
begin
if (SourcesType.AsInteger = DBSOURCETYPE_DATASOURCE) and (SourcesName.AsString <> '') then
Names.Add(SourcesName.AsString);
Next;
end;
finally
Names.EndUpdate;
end;
finally
Free;
end;
end;

procedure TOpenSQLServerForm.DatabasesOnServer(Databases : TStrings);
var
rs : _RecordSet;
begin
Databases.Clear;
with TAdoConnection.Create(nil) do
try
ConnectionString := ConnStr;
LoginPrompt := False;
try
Open;
rs := ConnectionObject.OpenSchema(adSchemaCatalogs, EmptyParam, EmptyParam);
with rs do
begin
try
Databases.BeginUpdate;
while not Eof do
begin
Databases.Add(VarToStr(Fields['CATALOG_NAME'].Value));
MoveNext;
end;
finally
Databases.EndUpdate;
end;
end;
Close;
except
on e:exception do
MessageDlg(e.Message,mtError, [mbOK],0);
end;
finally
Free;
end;
end;



procedure TOpenSQLServerForm.rbLoginInfoClick(Sender: TObject);
begin
ledUserName.Enabled := True;
ledPassword.Enabled := True;
end;

procedure TOpenSQLServerForm.rbIntegratedSecurityClick(Sender: TObject);
begin
ledUserName.Enabled := False;
ledPassword.Enabled := False;
end;

procedure TOpenSQLServerForm.cboServersClick(Sender: TObject);
begin
DatabasesOnServer(cboDatabases.Items);
end;

procedure TOpenSQLServerForm.btnOKClick(Sender: TObject);
begin
TestConButtonClick(Sender);

if (sc.ServerName = '') OR (sc.DatabaseName = '') then
begin
ModalResult:= mrNone;
Exit;
end;
end;

procedure TOpenSQLServerForm.TestConButtonClick(Sender: TObject);
var
dbc : TAdoConnection;
begin
dbC := TAdoConnection.Create(nil);
try
dbc.LoginPrompt := False;
dbc.ConnectionString := ConnStr;

if (sc.ServerName = '') OR (sc.DatabaseName = '') then
begin
MessageDlg('Select at least a Server and a Database!', mtWarning, [mbOK],0);
Exit;
end;

try
dbc.Open;
dbc.Close;
MessageDlg('Connection successful!',mtInformation, [mbOK],0);
except
on e:exception do
MessageDlg(e.Message,mtError, [mbOK],0);
end;
finally
if dbc.Connected then dbc.Close;
dbc.Free;
end;
end;


function TOpenSQLServerForm.GetConnStr: widestring;
begin
SC.ServerName := cboServers.Text;
if cboDatabases.ItemIndex <> -1 then
SC.DatabaseName := cboDatabases.Items[cboDatabases.ItemIndex]
else
SC.DatabaseName := '';
SC.UserName := ledUserName.Text;
SC.Password := ledPassword.Text;

Result := 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;';
Result := Result + 'Data Source=' + SC.ServerName + ';';
if SC.DatabaseName <> '' then
Result := Result + 'Initial Catalog=' + SC.DatabaseName + ';';

if rbLoginInfo.Checked then
begin
Result := Result + 'uid=' + SC.UserName + ';';
Result := Result + 'pwd=' + SC.Password + ';';
end

end;

procedure TOpenSQLServerForm.FormCreate(Sender: TObject);
begin
Screen.Cursor := crSQLWait;
try
ListAvailableSQLServers(cboServers.Items);
finally
Screen.Cursor := crDefault;
end;
end;

procedure TOpenSQLServerForm.btnCancelClick(Sender: TObject);
begin

end;

end.
 
做一个类似设置SQLServer连接的窗口。
ListAvailableSQLServers 是列出所有可利用的SQLServer服务器
GetConnStr 生成连接串
DatabasesOnServer 连出SQLServer服务器上的数据表
 
这个我也知道啊,就想知道每一个fun里面是怎么回事
 
这个程序没有使用delphi提供的ADO组件,而是使用了原生的ADO(是com组件)
功能如hxy2002所说。
若是对com编程不了解,估计看起来费劲。
声明:我对com编程不太了解
 
http://www.delphibbs.com/keylife/iblog_show.asp?xid=13813
你看看我写的这个,可能会对你有所帮助
 
看过了,还是不太明白哟,没做这方面的研究,还请指教
 
后退
顶部