组态王查询数据库总结
一、KVADO控件查询数据库数据
1.按日期时间查询
string whe; whe="日期时间>=#"+\local开始日期时间组合+"# and 日期时间<=#"+\local结束日期时间组合+"# "+"ORDER BY 日期时间 ASC" ; Trace(whe = %2s, whe); Ctrl0.Where=whe; Ctrl0.FetchData(); Ctrl0.FetchEnd();
2.按日期查询
string whe; whe="日期>=#"+\local开始日期+"# and 日期<=#"+\local结束日期+"# "+"ORDER BY 日期 ASC" ; Trace(whe = %2s, whe); Ctrl0.Where=whe; Ctrl0.FetchData(); Ctrl0.FetchEnd();
3.按某个字段查询
1 根据字段名查询
查询.Where="TagName=原料油液位"; 查询.FetchData(); 查询.FetchEnd();
2 使用整型变量查询
string whe; whe="温度="+StrFromInt( \local查询数值, 10 ); Trace(whe = %2s, whe); Ctrl0.Where=whe; Ctrl0.FetchData(); Ctrl0.FetchEnd();
3使用字符串变量查询
string whe; whe="测试字符串="+\local测试字符串+""; Trace(whe = %2s, whe); Ctrl0.Where=whe; Ctrl0.FetchData(); Ctrl0.FetchEnd();
3多条件查询
string whe; whe="日期="+""+\local日期+""+"and 班次="+""+\local查询班次+""; Trace(whe = %s,whe); 查询.Where=whe; 查询.FetchData(); 查询.FetchEnd();
5自定义查询
Ctrl0.QueryDialog(); Ctrl0.FetchData(); Ctrl0.FetchEnd();
6.全部查询
Ctrl0.FetchData(); Ctrl0.FetchEnd();
二、SQLselect函数查询
使用SQLselect函数查询查询需要建记录体,关联内存变量,再结合SQLselect查询。
1.按日期
string whe; whe="日期>="+\local开始日期+""+"and 日期<="+\local结束日期+""; SQLSelect( DeviceID, "表格1", "记录体2", whe, "时间 DESC" );
2.按日期时间
string whe; string whe1; string whe2; whe="日期>="+\local开始日期+""+"and 日期<="+\local结束日期+""; whe1="and 时间>="+\local开始时间+""+"and 时间<="+\local结束时间+""; whe2=whe+whe1; Trace(test = %2s, whe2); SQLSelect( DeviceID, "表格1", "记录体2", whe2, "时间 DESC" );
3.按字符串
//按字符串 //string whe; //whe="测试字符串="+\local测试字符串+""; //Trace(whe= %2s, whe); SQLSelect( DeviceID, "表格1", "字符串", "", "" );
4.按整型变量
string whe; whe="温度="+StrFromInt( \local查询数值, 10 ); Trace(whe= %2s, whe); SQLSelect( DeviceID, "表格1", "整数变量", whe, "" );
5.多条件查询
String str="组件号="+""+\local组件号+""+"and 层号="+\localB ; SQLSelect( DeviceID, "Table3", "BIND2_Q", str,"日期时间 ASC" );
其中,组件号是字符串变量,层号是整型变量。
