Common
Script撰寫時機
常用方法
SQLConnect( ConnectionId, ConnectString );
SQLSelect( ConnectionId, TableName, BindList, WhereExpr, OrderByExpr );
SQLFirst( ConnectionId );
SQLInsert( ConnectionId, TableName, BindList );
範例1:建立SQL連線
- 建立Bind List (參考EP5)
- 建立點位ConnectionId (Memory Integer), ResultCode, ErrorMsg (Memory Message)
- 撰寫Script於Application On Startup
- 利用LogMessage可以於SMC紀錄訊息
ResultCode = SQLConnect( ConnectionId, "Provider=SQLOLEDB; DATA SOURCE=127.0.0.1; UID=sa; PWD=sa; DataBase=data;" );ErrorMsg = SQLErrorMsg(ResultCode);LogMessage("SQL connect, Error msg:" + ErrorMsg);
範例2:停止SQL連線
- 撰寫Script於Application On Shutdown
ResultCode = SQLDisconnect( ConnectionId ); ErrorMsg = SQLErrorMsg(ResultCode); LogMessage("SQL disconnect, Error msg:" + ErrorMsg);
範例3:Select資料
- 常撰寫Script於Application While Running或Data Change (常利用系統Tag,例如:每秒或每分鐘更新一次資料)
- SQLSelect( ConnectionId, TableName, BindList, WhereExpr, OrderByExpr );
- 範例:
ResultCode=SQLSelect(ConnectionID,"BATCH","List1","type='cookie'","amount ASC,sugar DESC");
- 注意:若寫在不同的觸發時機的Script (Application On Startup, Application While Running),有可能發生資料庫連線尚未建立,就去資料庫Select資料的狀況,當然撈回來的資料都是0
- 注意:字串類型的資料有字數限制,InTouch Message tags are limited to 131 characters.
此範例為以時間排序,撈出最新一筆資料
SQLSelect(ConnectionId, "dbo.data", "data_bind_list", "", "date DESC, time DESC"); ResultCode = SQLFirst(ConnectionId); ErrorMsg = SQLErrorMsg(ResultCode); LogMessage("SQL select, Error msg:" + ErrorMsg);
沒有留言:
張貼留言