使用delphi枚举本地网络上的Microsoft SQL数据库服务器 [英] Enumerate Microsoft SQL database servers on the local network, using delphi
问题描述
如果我使用C#,我可以使用.net框架的 SqlDataSourceEnumerator ,以发现和显示用户网络上SQL Server实例的列表。
如何在Delphi中执行此操作? / p>
要枚举所有可用的Microsoft SQL Server,您可以按照这个优秀的教程:
枚举可用的SQL Server。在SQL Server上检索数据库
包含在 Zarko 的教程,有一个链接到下载完整的源代码,这对于快速测试它是有用的,并检查它是否符合您的需求。
编辑 Zarko Gajic的主要例程是:
procedure ListAvailableSQLServers(Names:TStrings);
var
RSCon:ADORecordsetConstruction;
Rowset:IRowset;
SourcesRowset:ISourcesRowset;
SourcesRecordset:_Recordset;
SourcesName,SourcesType:TField;
函数PtCreateADOObject(const ClassID:TGUID):IUnknown;
var
状态:HResult;
FPUControlWord:Word;
begin
asm
FNSTCW FPUControlWord
end;
状态:= CoCreateInstance(
CLASS_Recordset,
nil,
CLSCTX_INPROC_SERVER或CLSCTX_LOCAL_SERVER,
IUnknown,
Result);
asm
FNCLEX
FLDCW FPUControlWord
end;
OleCheck(Status);
结束
begin
SourcesRecordset:= PtCreateADOObject(CLASS_Recordset)as _Recordset;
RSCon:= SourcesRecordset作为ADORecordsetConstruction;
SourcesRowset:= CreateComObject(ProgIDToClassID('SQLOLEDB枚举器))作为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'); {不要本地化}
SourcesType:= FieldByName('SOURCES_TYPE'); {不要本地化}
Names.BeginUpdate;
尝试
而不是EOF做
开始
如果(SourcesType.AsInteger = DBSOURCETYPE_DATASOURCE)和(SourcesName.AsString<>'')然后
Names.Add (SourcesNameAsString);
下一个;
结束
finally
Names.EndUpdate;
结束
终于
免费;
结束
结束
我不知道我可以添加什么Zarko解释说:>>
If I was using C# I could use the .net framework's SqlDataSourceEnumerator to discover and show a user a list of SQL Server instances on the network.
How can I do that in Delphi?
To enumerate all available Microsoft SQL Servers, you can follow this excellent tutorial:
Enumerating available SQL Servers. Retrieving databases on a SQL Server
Included in Zarko's tutorial, there's a link to download the full source code which can be useful to quickly test it and check if it fits your needs.
Edit Zarko Gajic's main routine is:
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;
I don't know what I can add without lamering what Zarko's explained.
这篇关于使用delphi枚举本地网络上的Microsoft SQL数据库服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!