请帮忙翻译delphi to BCB ( 积分: 100 )

P

pascal

Unregistered / Unconfirmed
GUEST, unregistred user!
function GetSQLServerList(var AList: TStrings): Boolean ;
var
SQLServer : Variant;
ServerList: Variant;
i: Integer;
begin
Result := True;
try
SQLServer := CreateOleObject('SQLDMO.Application');
ServerList:= SQLServer.ListAvailableSQLServers;
if serverlist.count>0
then
for i:=1 to ServerList.Countdo
AList.Add(ServerList.Item(i))
else
Result := false ;
SQLServer := NULL ;
ServerList := NuLL ;
except
Result := False;
end;
end;

译文:
bool __fastcall GetSQLServerList(TStrings *AList)
{
Variant SQLServer;
Variant ServerList;
bool Result = true;
try
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
// Coinitialize(NULL);
SQLServer = CreateOleObject("SQLDMO.Application");
ServerList= SQLServer->ListAvailableSQLServers;
//这里编译不过:pointer to structure required on left side of -> or ->*
/*
这样也不行:
ServerList= SQLServer.ListAvailableSQLServers;
//ListAvailableSQLServers is not a member of 'Variant'
ServerList= (_di_IDispatch *)SQLServer->ListAvailableSQLServers;
//pointer to structure required on left side of -> or ->*
*/
if (0 < serverlist.count)
for (i=1;ServerList.Count;i++) AList.Add(ServerList.Item(i))
else
Result = false ;
SQLServer = NULL ;
ServerList = NULL ;
CoUninitialize();
}
catch (...)
{ Result = False;}
return Result;
}
 
function GetSQLServerList(var AList: TStrings): Boolean ;
var
SQLServer : Variant;
ServerList: Variant;
i: Integer;
begin
Result := True;
try
SQLServer := CreateOleObject('SQLDMO.Application');
ServerList:= SQLServer.ListAvailableSQLServers;
if serverlist.count>0
then
for i:=1 to ServerList.Countdo
AList.Add(ServerList.Item(i))
else
Result := false ;
SQLServer := NULL ;
ServerList := NuLL ;
except
Result := False;
end;
end;

译文:
bool __fastcall GetSQLServerList(TStrings *AList)
{
Variant SQLServer;
Variant ServerList;
bool Result = true;
try
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
// Coinitialize(NULL);
SQLServer = CreateOleObject(&quot;SQLDMO.Application&quot;);
ServerList= SQLServer->ListAvailableSQLServers;
//这里编译不过:pointer to structure required on left side of -> or ->*
/*
这样也不行:
ServerList= SQLServer.ListAvailableSQLServers;
//ListAvailableSQLServers is not a member of 'Variant'
ServerList= (_di_IDispatch *)SQLServer->ListAvailableSQLServers;
//pointer to structure required on left side of -> or ->*
*/
if (0 < serverlist.count)
for (i=1;ServerList.Count;i++) AList.Add(ServerList.Item(i))
else
Result = false ;
SQLServer = NULL ;
ServerList = NULL ;
CoUninitialize();
}
catch (...)
{ Result = False;}
return Result;
}
 
还真的不熟BCB, 若是C -> PASCAL, 倒可以帮忙, :~~)
 
捡麻子……
 
BCB 就是C++语法啊,我熟,
明天帮你,现在回家了,刚才排练大合唱累死了。。
 
SQLServer := NULL ;
ServerList := NuLL ;
这两句应该不认把?改成nil试试。
function GetSQLServerList(var AList: TStrings): Boolean ;
参数里面的var去掉,Delphi的对象都是传值的。
 
楼主, 帮您问了一位超超超超超级高手, 供参考, [:)][:)]
----------------------
yzhshi 21:15:41
照猫画虎,从这里面应该能得到答案
http://www.programfan.com/article/showarticle.asp?id=1585
 
另一位热心人, [:)][:)]
-------------------
(2005-12-19 20:39:17) oliver(892143x)
file://设置Excel为不可见
Ex.OlePropertySet(&quot;Visible&quot;,false);
file://打开指定的Excel报表文件。报表文件中最好设定只有一个Sheet。
Ex.OlePropertyGet(&quot;WorkBooks&quot;).OleProcedure(&quot;Open&quot;,ExcelFileName.c_str());
Wb = Ex.OlePropertyGet(&quot;ActiveWorkBook&quot;);
Sheet = Wb.OlePropertyGet(&quot;ActiveSheet&quot;);//获得当前默认的Sheet
(2005-12-19 21:15:21) 刘麻子(71892967)
谢谢
(2005-12-19 21:18:05) oliver(892143x)
C++builder在调用OLE的时候与DEPHI有很大不同。[:)]
(2005-12-19 21:19:10) 刘麻子(71892967)
[share]
 
引用OleCtnrs.hpp,在BCB2006下编译通过.低版本的我没有.应该也没有问题
BOOL GetSQLServerList(TStrings *AList)
{
Variant SQLServer;
Variant ServerList;
BOOL Result = TRUE;
int c;

AList->Clear();
SQLServer = CreateOleObject(&quot;SQLDMO.Application&quot;);
ServerList = SQLServer.OleFunction(&quot;ListAvailableSQLServers&quot;);
c = ServerList.OlePropertyGet(&quot;Count&quot;);
if (0 < c)
for (int i=1;
i<= c;i++)
{
AList->Add(ServerList.Exec(Function(&quot;Item&quot;) << i));
}
else
Result = FALSE;
Result = TRUE;
SQLServer = NULL;
ServerList = NULL;
return Result;
}
 
谢二位.试下先
Microsoft Excel作为一个功能强大的电子表格处理软件,已经得到了广泛的应用。在数据库应用软件开发过程中,如果能实现数据库与Excel文件的数据交换,那么一方面能增加数据库数据的来源,另一方面也能方便用户对数据库数据的进一步处理。
  C++ Builder是一个可视化快速应用程序开发工具,它提供的OLE Automation机制使得开发人员在应用程序中就能调用Excel,实现数据交换。
  在C++ Builder中调用Excel,首先要为Excel建立一个OLE对象,然后通过设定该对象的属性和调用该对象的方法来操纵Excel。C++ Builder通过CreateOleObject()来建立OLE对象;用OlePropertySet(propname,value)来设置OLE对象的属性;用OlePropertyGet(propname)获得OLE对象的属性;通过OleFunction(oleFuncName,[val,...])和OleProcedure(oleProcName,[val,...])来调用OLE对象的方法。
  C++ Builder中使用OLE自动化操纵Excel,必须掌握Excel的自动化对象及VBA关于Excel的对象方法和属性,这些都在Microsoft Office(完全安装)的VBAXL8.HLP帮助文件中可以找到,在此就不再赘述。下面以将数据库中的数据转到Excel工作薄中为例,说明C++ Builder操纵Excel的具体方法。
  首先新建一个表单Form1,保存单元文件Unit1.cpp,保存工程文件Project1.bpr。然后在表单中加入数据访问控件TTable,将Name属性设为Table1,DatabaseName属性设为BCDEMOS,TableName属性设为Country.db。在表单中加入一个按钮控件TButton,将其Name属性设为Button1,Caption属性设为 “转换为Excel文件”。双击Button1,在Button1Click()函数中加入如下代码:
   Variant ex,newxls;

   int i,j=1;

try
{
ex=CreateOleObject(″Excel.Application″);
//启动Excel
}
catch(...)
{
ShowMessage(″无法启动Excel″);

}
   ex.OlePropertySet(″Visible″,(Variant)true);
//使Excel启动后可见
newxls=(ex.OleFunction(″Workbooks″)).OleFunction(″Add″);//新建一个工作薄
Table1-〉Active=true;  
  //打开数据库
Table1-〉First();

for(i=0;i〈Table1-〉FieldCount;i++)
  //将字段名写到工作薄的第一行
{
(ex.OleFunction(″Cells″)).OlePropertySet(″Item″,(Variant)1,(Variant)(i+1),(Variant)Table1-〉Fields-〉FieldName);

}
while(!Table1-〉Eof)
  //将数据库中的记录依次写到工作薄中
{
j=j+1;

for(i=0;i〈Table1-〉FieldCount;i++)
{
  (ex.OleFunction(″Cells″)).OlePropertySet(″Item″,(Variant)j,(Variant)(i+1),
  (Variant)Table1-〉Fields-〉AsString);

}
Table1-〉Next();

}
newxls.OleFunction(″SaveAs″,(Variant)filename);

  //保存工作薄,filename是工作薄的全文件名
ex.OleFunction(″Quit″);

  //退出Excel,释放OLE对象
  注意,要使用OLE自动化对象,在编译之前还必须在Unit1.cpp文件前面加上#include ″ComObj.hpp″。
  运行程序就可以实现将数据库中的数据转换到Excel工作薄中。同样的道理,使用OlePropertyGet()函数,也可以将Excel工作薄中的数据读到数据库中去。
 
to wr960204,刘麻子:再邦看这二贴
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3293895
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3302581
 
BCB 不是 直接 可以 引用 *.pas
为什么 还要 转换?
 
嘿嘿嘿,这个问题我可没帮多少忙,分数应该给wr960204大哥,我就不去了,受之有愧~~ [:)][:)]
 
麻子客气了。楼主给你分数是正常的。因为要不是你在QQ上通知我来解决这个问题,楼主的问题怎么会得到解决呢
 
hfghfghfg 说的没错啊,把这个函数写到一个 .pas 文件里面,
直接加入 BCB的工程,然后直接用 C++ 语法调用这个函数就可以了
再提供另外一种思路,我们可以抛弃 Ole 方式来实现此功能,
Ole方式实现的具体细节是,在局域网发出UDP广播,到 1434 端口
然后根据应答的数据包,来判断SQLServer 的信息
数据包的格式很简单,也很容易解析, 用Sniffer 抓几个包看看就明白了,
我以前就用UDP 方式写过这个功能
 
哦也, 楼上厉害, [:)][:)]
 

Similar threads

I
回复
0
查看
553
import
I
I
回复
0
查看
733
import
I
I
回复
0
查看
822
import
I
顶部