搬家第14天-143.Wincc V7.3使用c脚本将sql server数据表内容显示在ListView控件中,并添加文字
在142练习中,虽然把数据表字段分级装入了TreeView控件,但是我们看到数字前面没有标注flow1、flow2和flow3,如果我们希望最终展示出来的效果要加上注释,那么也很简单,只需要稍稍修改一下sql语句即可(已经标注红色)。
新建四个内部变量,均为文本型,名字分别为CurDate、flow1、flow2、flow3。画面上放置一个treeview控件,名字为TV,放置一个按钮,在按钮的鼠标点击事件添加以下c脚本:
#include "apdefap.h"
void OnClick(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName)
{
#pragma code("kernel32.dll")
#define GetObject GetObject
int i,RsCount;
__object *pdl=NULL;
__object *pic=NULL;
__object *tv=NULL;
__object *NodeIndex;
__object *cn=NULL;
__object *rs=NULL;
char sql[256],Relative[10],key[10],txtDate[10];//flow1[20],flow2[20],flow3[20];
char *connstr="Provider=SQLOLEDB.1;password=sa;user id =sa;Initial Catalog=Report;Data Source=.\\WINCC";
pdl = __object_create("PDLRuntime");
pic=pdl->GetPicture("");
tv=pic->GetObject("TV");
cn=__object_create("ADODB.Connection");
rs=__object_create("ADODB.RecordSet");
sprintf(sql,"%s","select CurDate,'flow1:'+Convert(varchar(10),flow1),'flow2:'+Convert(varchar(10),flow2),'flow3:'+Convert(varchar(10),flow3) from report" );
cn->open(connstr);
if (cn->State == 0)// 为0表示连接数据库不成功
MessageBox(NULL,"连接数据库失败!","警告",MB_OK);
else
{
rs->Open(sql,cn,3,1);
RsCount=rs->RecordCount;
rs->MoveFirst();
tv->Nodes->Clear(); //先清空
tv->Style=6; //6.直线、+/-和文本;
tv->LineStyle =1;
//treeview填入数据
//第一级节点
NodeIndex=tv->Nodes->Add();;
NodeIndex->key="Class0";
NodeIndex->Text="Report数据表";
//填入第二季节点-“日期时间”
for(i=1;i<=RsCount;i++)
{
sprintf(key,"%s%d","Class",i);
SetTagChar("CurDate",rs->fields(0)->value);
sprintf(txtDate,"%s",GetTagChar("CurDate"));
NodeIndex=tv->Nodes->Add("Class0",4);
NodeIndex->key=key;
NodeIndex->Text=txtDate;
rs->MoveNext();
}
//填入第三级节点flow1,flow2,flow3
rs->MoveFirst();
for(i=1;i<=RsCount;i++)
{
sprintf(Relative,"%s%d","Class",i);
SetTagChar("flow1",rs->fields(1)->value);
SetTagChar("flow2",rs->fields(2)->value);
SetTagChar("flow3",rs->fields(3)->value);
//添加flow1
NodeIndex=tv->Nodes->Add(Relative,4);
sprintf(NodeIndex->key,"%s%d%s","Class",i,"1");
NodeIndex->Text=GetTagChar("flow1");
//添加flow2
NodeIndex=tv->Nodes->Add(Relative,4);
sprintf(NodeIndex->key,"%s%d%s","Class",i,"2");
NodeIndex->Text=GetTagChar("flow2");
//添加flow3
NodeIndex=tv->Nodes->Add(Relative,4);
sprintf(NodeIndex->key,"%s%d%s","Class",i,"3");
NodeIndex->Text=GetTagChar("flow3");
rs->MoveNext();
}
for (i=1;i<=tv->nodes->count;i++)
if (tv->nodes->item(i)->Children>0) tv->nodes->item(i)->Expanded=TRUE;
}
if(cn->State != 0)
cn->Close;
__object_delete(tv);
__object_delete(pic);
__object_delete(pdl);
}